Execute Jar from Java code - java

I have this code that is supposed to run a executable jar, but whenever the code is executed nothing happens?
try {
proc = Runtime.getRuntime().exec("java -jar C://X-Dock//MP3Player.jar");
} catch (IOException e1) {
e1.printStackTrace();
}
The JAR works fine if I run it manually, but that line of code just doesn't work. And I know for sure that the code is called.

If you has a JRE after version 5, java provides a process builder. So, try something like this:
final ProcessBuilder pBuilder = new ProcessBuilder("/java/path", "-jar", "your_jar.jar");
pBuilder.directory(new File("your/working/directory"));
final Process process = pBuilder.start();
"/java/path" is path for java installation, can be replaced by java, if java is in the environment variables.
See more at ProcessBuilder.

Related

Error when creating zstd file with ProcessBuilder on Java

I'm trying to use ProcessBuilder in order to create a tar.zst file that compresses a folder. However, I'm getting the following error after running this code:
try {
ProcessBuilder pb = new ProcessBuilder("tar","--zstd","-cf","info-x.tar.zst","tstpkg");
pb.redirectOutput(ProcessBuilder.Redirect.INHERIT);
pb.redirectError(ProcessBuilder.Redirect.INHERIT);
Process process = pb.start();
process.waitFor();
} catch (Exception e){
e.printStackTrace();
}
Error:
tar: Can't launch external program: zstd --no-check -3
And the tar file is created, but is empty.
If I run the same command from the terminal
tar --zstd -cf info-x.tar.zst tstpkg
then it works fine.
When you find a command that works "from the terminal" but not from Java then you should assume that the PATH or other environment required with that command is only set correctly via that terminal, and not when called by JVM.
So, one possible fix is to run your terminal indirectly which may fix the environment needed - depending on your shell and it's setup:
ProcessBuilder pb = new ProcessBuilder("bash", "-c", "tar --zstd -cf info-x.tar.zst tstpkg");
You might also try to fix by setting the correct PATH for running zstd as a sub-process of tar. Check where it is using which zstd and try adding to the PATH before pb.start():
String path = pb.environment().get("PATH"); // use Path on Windows
pb.environment().put("PATH", "/path/to/dir/ofzstd"+File.pathSeparator+path);

nohup doesn't write anything to file

i have a Java program which executes the following shell script to restart it self.
sleep 5
nohup java -jar /home/my-dir/MyJar.jar &
If i run the script from a terminal, it just works as expected. However if the Java Program executes the script, the program starts normally but nothing gets written to the output file.
I start the script via the following code
ProcessBuilder processBuilder = new ProcessBuilder();
processBuilder.command("/bin/sh", "/home/my-dir/start.sh");
try {
processBuilder.start();
logger.info("Successfully started");
} catch (IOException e) {
e.printStackTrace();
}
Following command worked for me. It writes all the output to the specified file.
nohup java -jar /home/my-dir/MyJar.jar > /home/my-dir/log.txt & tail -f /home/my-dir/log.txt &

Java calls wrong python version

I'm trying to run a Python script from a Java program using Process and ProcessBuilder, however Java keeps using the wrong version of Python. (The script needs 3.6.3 to run and Java runs Python 2.7)
However when I run the script from the terminal (outside of Java), it runs the correct Python (3.6.3). How does one change what version of Python gets run when called by Java?
The short version is it changes with your PATH environment variable.
Under Windows, Technet has the answer. Scroll down to the 'Command Search Sequence' section. This answer explains it nicely.
For UNIX-like OS's, this answer is nicely detailed.
There are two very useful commands for determining which executable is going to be called: which for UNIX-likes and where for newer Windows.
The most likely reason for the difference between Java and the terminal is a difference in your PATH. Perhaps your Java version is being run with a modified PATH? A launch script of some kind may be changing it.
Add /usr/bin/python3.4 to the start of your command to force the version of python you want. If you're not sure where python is installed, have a go at using whereis python and seeing what you get back.
private int executeScript(final List<String> command) {
try {
final ProcessBuilder processBuilder = new ProcessBuilder("/usr/bin/python3.4").command(command);
processBuilder.redirectErrorStream(true);
System.out.println("executing: " + processBuilder.command().toString());
final Process process = processBuilder.start();
final InputStream inputStream = process.getInputStream();
final InputStream errorStream = process.getErrorStream();
readStream(inputStream);
readStream(errorStream);
return process.waitFor();
} catch (Exception e) {
System.out.println(e.getMessage());
}
return -1;
}
Then just pass in the List containing your python commands.

Change PWD of linux from JSP

I need to execute linux commands from JSP.
It is working fine.
But i need to start some sh file in a particular directory in linux through JSP. say /home/username/something/start.sh
try{
String command= "cd /home/username/something";
Runtime.getRuntime().exec(command);
Runtime.getRuntime().exec("./start.sh")
out.println("Child");
}
catch(Exception e)
{ out.println("Error");
}
It says FIle or Directory not found.
I tried Runtime.getRuntime().exec("pwd"), It is showing something like "java.lang.UNIXProcess#fc9d2b" !! :O
I need to change the pwd and execute some commands through jsp. How can i do that??
Any help would be appreciated.
You shouldn't (and actually, it seems you can't) set a working directory like that. Each Process object given by Runtime.exec() will have its own working directory.
As answered in How to use “cd” command using java runtime?, you should be using the three argument version of Runtime.exec(), in which you provide a File that will be the working directory. From its javadoc:
Executes the specified command and arguments in a separate process with the specified environment and working directory.
Or even better, use ProcessBuilder along with ProcessBuilder.directory() instead:
ProcessBuilder pb = new ProcessBuilder("start.sh");
pb.directory(new File("/home/username/something"));
Process p = pb.start();

Only part of script executed from Java program

I have tried running a shell script from a Java program, but the entire script is not being executed. And idea why we might come across such problem?
The Java code to execute shell script:
File file = new File("/path/to/script");
String COMMAND= "./run";
ProcessBuilder p = new ProcessBuilder(COMMAND);
p.directory(file);
try {
Process startProcess= p.start();
} catch (IOException e) {
e.printStackTrace();
}
The script runs fine but not whole script is executed. It seems like only the 1st line is being executed.
If you are sure that the script starts running the problem is not in java but in script itself.
The reason of difference may be the wrong path or wrong environments. When you are running script from console you are in your user's environment, so script can use all environment variables.
Try to add some debug outputs to figure the problem out.

Categories