Automate JAR execution that requires python interpreter - java

Scenario : I have a Referee.jar program which I got from somewhere (making a point that I don't know to change that code). Also, I have 2 python files which I've written.
Currently : The JAR file has to be executed first in the terminal with "java -jar referee.jar" and then "python 1.py" and "python 2.py" have to be typed into the following lines.
Requirement : I want to make a shell script which will do that and store the output into a file called 'out.txt'

Found the answer here.
Automatically answer to input prompt in windows batch
Just had to echo the file names and redirect it to the command.
My sincere apologies for not checking this before posting the question.

Related

Windows .bat file starting java command : how to find it in process list

I have a .bat file composed of a single java command : java -jar someProgram.jar.
This .bat file is executed by a nodejs process, using the os.exec command. Before executing the script, I want to check if it has already been executed, so I'm listing the processes list and I search for "someProgram.jar", but I can't find it, because it seems to appear under a single 'java' name.
Is there a way to get processes list with more informations (like the full command), or any other solution ?
Thanks

Run a java program from anywhere

I am running a java program using a .bat file. It works well after double clicking direcctly on the .bat, that's not the problem.
What I want now, is to run that .bt file (and the java program by extends) from the cmd at first, and then to be able to compile it from any other machine.
I have followed at first the following answer : How do I run a java program from a different directory? , but it didn't work for me.
Here is my .bat file :
#echo on
set CLASSPATH=%CLASSPATH%;.;lib/console.jar;lib/log4j-1.2.13.jar;lib/prog1.jar;lib/prog1_newOption.jar;lib/org.hamcrest.core_1.3.0.v201303031735.jar;lib/RXTXcomm.jar;lib/trace.jar;lib/xercesImpl.jar;lib/xml-apis.jar
java -cp "$/prog1_newOption/src/main/Main" %UsersCommand%
pause
exit
%cmd%
Maybe have I to look for the main path in the .bat file and then parse it the the "java command programm" line?
Thank you so much for your help

Java JAR file does not execute in startup script in Ubuntu 14.04

The following process normally works for my startup scripts. However, when I introduce a command to execute a JAR file, it does not work. This script works while I am logged in. However, it does not work as a startup script.
In /etc/init.d I create a bash script (test.sh) with the following contents:
#!/bin/bash
pw=$(curl http://169.254.169.254/latest/meta-data/instance-id)
pwh=$(/usr/bin/java -jar PWH.jar $pw &)
echo $pwh > test.txt
Make script executable
In /etc/rc.local, I add the following line:
sh /etc/init.d/test.sh
Notes:
I make a reference to the script in /etc/rc.local, because this script needs to run last after all services have started.
Please do not ask me to change the process (i.e., create script in /etc/init.d/ and reference it from /etc/rc.local), because it works for my other startup scripts.
I have tried adding nohup in front of java command, and it still did not work.
Thanks
As written, there is insufficient information to say what is going wrong. There are too many possibilities to enumerate.
Try running those commands one at a time in an interactive shell. The java command is probably writing something to standard error, and that will give you some clues.

How to pass parameters from command line to a java program through makefile

My first question in stack overflow... Kind of excited but still struggling in the problem.
Alright, My question is how to pass parameters from command line to a java program through makefile.
Honestly I don't really know wether my description is correct....... Cause I don't really know much about makefile... In my assignment, the description is that we must develop a Makefile for GNU make to build our program. For example, the command lines
make
mipsim -v < test1.cmd > test1.log
will build the ISS (a simulator we made) and then run it with debugging output, taking input commands from the file test1.cmd and writing result to test1.log.
I have finished the program but I don't know how to make the things above happen.
What I know so far is just to use makefile to make the .class file from .java file....
I have no idea about how to get test1.cmd as my input file's name and test1.log as my output
file's name from command lines.... I guess these two names probably will get into my program through String[] args in the main function...
Could anybody give me some help please?
Thanks
There is some confusion as to the issues.
First, compile Java using make is a little... iffy. (Most people use ant or maven.) However, if you don't mind a little overhead, you can do it using make. You probably should run make from a directory at the root of the Java package hierarchy. You can determine all Java files below using make macros. Hint: shell:
JAVA_FILES = $(shell find -name \*.java)
Then you run javac. (Make sure to define all path names to compilers etc. using make macros.) With Java, it's not easy to derive a make target, because .class files are not 1:1 w.r.t. java files. I just use a target "compile", depending on all the java files, and touch a file acting as a dummy target.
Second, the execution. To invoke a Java program that is not in an executable jar, you set the classpath (option -cp), specify the main class name and add command line parameters. I'd have to know what "mipsim" is - probably a shell script for doing just that. Anyway, a make target could be the log file:
%.log : %.cmd
${JAVA_HOME}/bin/java -cp ${ROOT} <$< >$#
Now, make test1.cmd should run your program.
Note: Redirection is not specified by program arguments; this is handled by the shell.
Quick comment on your question.
your makefile needs 2 targets. one for build and the other for the run.
all: build run
build:
(this is to build class file from your java source)
run:
put your java command line here like "java ..."
When you run "make", it will call "all" target. And all target will call "build" and "run" target, so just put one thing in one target and use the combinations.
Your java code.
Is your java takes input filename as argument or from stdin?
If you want to take input filename, then you can take it from args argument passed to your main(String[] args).
If you want to read from stdin then you can create a bufferedreader as below.
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Hope this help.
(+1 please, if you like this answer)
There are a bunch of unrelated questions..
The syntax you are showing:
mipsim -v < test1.cmd > test1.log
would call an executable mipsim. Pass "-v" to the args[1]. Redirect test1.cmd to be the standrad input and test1.log be the standard output.
The output input redirection happens by the operating system so in c++ reading from std::cin will read from the file and writing to std::cout will write to test1.log
In java these will be redirected to System.in and System.out
About makefiles
Basically a make file rule looks like this:
<target>: <dependency1> .. < dependencyn>
~tab~ command
So just like it is possible to build a target that calls Javac. It is possible to build a target that calls Java.. and so you can build a test target and use it to execute any command you need
If you built a c++ executable then you can execute it from the makefile in the same way.
test: mipsim
mipsim -v < test1.cmd > test1.log
Your final question about pass parameter values to command line from make file do you mean something like this?
make PARA1=1 PARA2=ABC.c
You can use the parameters in your makefile..
test: mipsim
mipsim -v < $(INPUT_FILE) > $(OUTPUT_FILE)

How to run a shell command through PHP code?

I am trying to run a Jar file in the backend of my php code.But I am not getting the desired output to it.There is a jar file which runs in the background and returns the Page Rank of any of the keyword and Domain given to it.
I am attaching the code,please suggest me any solution to it,because when I run it on the terminal,it is giving correct output.
Here is the Code :
<?php
set_time_limit(0);
function returnJarPath()
{
$jarPath = $_SERVER['DOCUMENT_ROOT'] . "myFolder/tools_new/includes/Rank.jar";
return $jarPath;
}
$jar = returnJarPath();
$command = "java -jar $jar aspdotnet/microsoft.com";//Passing the Argument to the Jar file.
$shellOutput = shell_exec($command);
print "The Shell Output is : " ; var_dump($shellOutput);print "<br />";
exec($command,$executeCommmand);
print "The Exec returns the value : " ; var_dump($executeCommmand);print "<br />";
passthru($command,$passthruCommand);
print "The Passthru returns the value : " . $passthruCommand. "<br />";
?>
I just checked apache's error log and the last error I found was :
sh: java: command not found
But as I have already said,I have been using the same command through SSH to run the Java command.So there's no such possibility of not having JAVA installed on the server.Please help me out of this mess...
If the jar file writes to standard output you can use exec.
Here is an example how I use it:
may be first: exec("cd jar dir"); // if jar fine needs to be executed from the same dir
$output = exec("/usr/bin/java -jar $jar aspdotnet/microsoft.com");
But as you say:
sh: java: command not found
It means the there is no path alias to java from php. Just use the full java path to the executable /usr/bin/java.
Given you are calling java. My bet is the output is being displayed in the Java Console, and not in the shell, where PHP could pull the text information.
How to solve this dilemma?
Well you could write the results to a file, if you have the java source to modify, and then read that file through php to get the results. The possibility of a collision here would be pretty good. The other option is to have Java connect to your MySQL database (if you had one) and then run the java then query the database for the response. Of course, you would need to pass Java a way for it to input the data to insert a record you could identify (a hash of some sort), I have never done that in Java, just a theory of how you might be able to do it.
Update
You may want to try the standard output as suggested by darko petreski as another option as well.
If the PHP code is to be executed in a server (and not via command line) the user that runs the java executable is www-data, not you. In that case make sure that www-data has the permissions to read the jar file and to execute the java executable
The first thing that I would check/change is the line in the function where you are building the $jarPath variable from this:
$jarPath = $_SERVER['DOCUMENT_ROOT'] . "myFolder/tools_new/includes/Rank.jar";
to this:
$jarPath = $_SERVER['DOCUMENT_ROOT'] . "/myFolder/tools_new/includes/Rank.jar";
The trailing slash may not be present in $_SERVER['DOCUMENT_ROOT'] which could cause issues.
I am assuming that when you say it runs from the console, you are running the java command like so:
$ java -jar /rest/of/path/myFolder/tools_new/includes/Rank.jar aspdotnet/microsoft.com
I would ensure that you include the path to the java binary in the $command variable like so...
$command = "/path/to/java -jar $jar aspdotnet/microsoft.com";
The user that owns the web server process may not have a $PATH variable that includes the path to the Java binary.

Categories