Running Jar files from Python - java

I want to make a program that can execute jar files and print whatever the jar file is doing in my python program but without using the windows command line, I have searched all over the web but nothing is coming up with how to do this.
My program is a Minecraft server wrapper and I want it to run the server.jar file and instead of running it within the windows command prompt I want it to run inside the Python shell.
Any ideas?

First you have to execute the program. A handy function for doing so:
def run_command(command):
p = subprocess.Popen(command,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
return iter(p.stdout.readline, b'')
It will return an iterable with all the lines of output.
And you can access the lines and print using
for output_line in run_command('java -jar jarfile.jar'):
print(output_line)
add also import subprocess, as run_command uses subprocess.

Related

How to make an executable File

Everybody probably has used install .exe files. But how to make them and when does it make sense to make one?
For example, I would have had programmed commercial software in Python, c++, etc. with different files a GUI and pictures and all of the other stuff.
When I want to deliver my product to my customer I don't want to give them a folder and say you need to install Python or Java and execute the program via your command line.
How can I create an executable file that installs the required language and sets up local instances and arranges all files into the correct order?
To create an executable file from a python program, there is pyinstaller. I don't know about java at the moment. The command is as follows :
pyinstaller fileName.py
You can add args (and there are 2 really helpful ones) :
pyinstaller --onefile -w fileName.py
--onefile will put everything into one single file (recommended) and -w will prevent the console from opening when running the .exe file. Add it if you're running a GUI or something. If you need to console for input, don't add -w.
If you want to automate a command line in cmd, create a shortcut leading to C:\Windows\system32\cmd.exe and add /k and your command. For example :
C:\Windows\system32\cmd.exe /k ipconfig
Double clicking on the shortcut will now run the cmd and execute ipconfig automatically. If you want more than one command, you can do command_1 && command_2 && ... && command_n
There is a setup program built into Windows.
Type
iexpress
and follow the wizard.
You need to provide code to run when it finishes to actually install the extracted files.;

Can't run java from PHP's exec

I've been trying to run a JAR file that would read from input.txt and write to output.txt this way in console:
java -jar file.jar input.txt output.txt
And it works 100% fine on my machine. I need to run it inside a php script, and this code works 100% fine for me (Mac OS, php built-in server):
exec("java -jar file.jar input.txt output.txt");
But once I deploy it (CentOS server) where the exec function is allowed, it fails, it returns an empty string and the jar does not work, running it directly from shell is OK.
How can I fix that?
Thanks in advance!
The problem with your exec() is that PHP doesn't know where Java is on the server. Update your command to specify the full path to the Java executable and it should work, though you should also use full paths to the jar and text files while you're at it.

Looping through jar in bash and storing console o/p in csv file

I have a shell script as follows. For some reason the op.csv file is not generated if there are multiple json files, i.e. java -jar is run multiple times within the for loop. However, if java -jar is run only once then the op.csv files gets created fine. Why is that?
for filename in $curr_dir/op/*.json; do
java -jar SCSVGEN.jar "$filename" >> 'op.csv'
sleep 2
done

Execute jar file with arguments from c++ and store output

I'm developing a project in visual c++ and at some point in code I need to execute some java jar program with some arguments and store it's output into a string
So the code I execute in CMD and it works is:
java -jar weka.jar something.arff
And I get some text returned to the command prompt
I want to do the same thing with c++ and store the output into a string variable
I tried using system function, but as far as I know it's impossible to store output using that one
I also tried using _popen, ( it did work with commands like dir etc...) but it didn't return any output when using the command provided above
Help would be appreciated

convert a java program into a linux command

I have created one java program on my Linux system which indents and formats the given file. I want to make that program work like a command in Linux which will take file names and other options as arguments and then will produce the output. I can do this with a C program by just copying the compiled executable in /bin folder but I don't know how to do it with java.
Sample script that can might further help-
#!/bin/bash
#Set whatever number of arguments you expect for the Java jar you have
ARGS_EXPECTED=3
if [ $# -ne $ARGS_EXPECTED ]
then
echo "[$HOSTNAME]: Usage: `basename $0` filename arg1 arg2"
exit 1
fi
java -cp yourfile.jar com.yourpkg.Driver $1 $2 $3
Save the above content to a file, say test.sh
and use the command to give an executable permission chmod +x test.sh
run like ./test.sh filename arg1 arg2 from current directory where test.sh is
I thing this can be useful for your case: http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/Documentation/binfmt_misc.txt?id=HEAD
You can simply run a Java class file or jar file with "java" command from command line. Example:
java -jar yourprogram.jar argument1 argument2
If you save this line in a text file saved for example as "script.sh" and then give it the permission for execution you can run it double clicking or from terminal typing ./script.sh in the same folder containing the file script.sh.
You can also produce scripts that use arguments with $1 $2 etc. avoiding the need of editing file.
http://www.linuxquestions.org/questions/linux-newbie-8/how-to-pass-command-line-parameter-to-shell-script-254396/
You can use named parameters, too.
You can also produce a C program for a new command like you suggested that run the "java" command. In this case you can introduce arguments directly from terminal and pass them to java command in the C source.
As others have pointed out it is probably best to use a small shell script to run the Java application. There are several open source products that will help you wrap your Java code to produce a runnable (set of) .jar(s).
If you have correctly separated your business logic from your interface (as you should) then it is probably best if your Java application parses the parameters given on the command line interface. To do this create a separate class for parsing such parameters and calling the classes making up the business logic. Of course this will lead quickly - if not immediately - in writing a parser for Linux like CLI parameters. When this happens you may wish to consider the Apache Commons CLI project.
If you don't want to use any wrapping application/runtime, my method is generally pointing to all the class file containers in the classpath and directly pointing to the class containing the static main method:
java -cp "path_to_jar;path_to_class_folder;etc" "nl.owlstead.stackoverflow.LinuxMain"

Categories