I'm trying to run a jar file from Java code, through exec().
The jar I'm executing have some resources relative to its path that need to be loaded. So for example executing from console:
java -jar [/path/to/jar/]exec.jar
is working only if the command is launched from the same directory
I've tried with: cd /path/to/jar/exec.jar && java -jar /path/to/jar/exec.jar
but it seems there is a problem within exec() for running cd, widely covered on the web. The main problem is that I'm looking for a procedure that runs both on Linux and Windows.
I've tried to mess with the -classpath option, but with no luck.
Is there any simple solution to this? Note that I'm not "fond" of the system call idea, it's just I was looking for a simple way to schedule the execution of custom jars.
Thank you in advance!
CB
You need to set the working directory of the launched process using ProcessBuilder.directory(File).
Related
There was a program that I used that made runnable .jar files.. All the ones I'm finding now are ones that make .exe files.. I remember it also has the option to make the file a .sh script as well. Anyone knows its name? I've been searching for hours with no avail :/
The command line
java -jar file.jar
Will run your jar file if it has a Main-Class defined as explained here.
You can use that command in a shell script.
You can create a runnable jar using NetBeans IDE or Eclipse IDE by just providing the main class to run. Rest of the things it will take automatically. That class must be having a main() method in it. Then you can run that jar file using java -jar yourjarfile.jar
Do you mean actually coding java and then compiling to .jar? If you do try
eclipse code editor
I used eclipse to make minecraft mods. It will work if you want to make .jar programs.
If you want to have a jar that you can execute using the usual syntax ./app.jar (instead of java -jar), here is a post explaining the process: how to create executable jars.
Basically, JAR is a variant of ZIP, which allows random bytes to be pre/appended to the JAR without corrrupting it. This means it is possible to prepend a launcher script at the beginning of the jar to make it "executable".
Here is a simple example of the process:
# Append a basic launcher script to the jar
cat \
<(echo '#!/bin/sh')\
<(echo 'exec java -jar $0 "$#"')\
<(echo 'exit 0')\
original.jar > executable.jar
# Make the new jar executable
chmod +x executable.jar
With this, you can now run ./executable.jar instead of java -jar original.jar. This works on all unix like systems including Linux, MacOS, Cygwin, and Windows Linux subsystem.
I'm having a strange issue trying to run classes from an executable .jar file on Linux that none of the existing question threads I've sorted through seem to be able to resolve. I'll preface this in that I've never had to use Linux before and am only using it in this situation out of necessity, so it's possible I have overlooked something simple that I just didn't know could be causing the problem.
I can launch the classes from my .jar file without any issues on Windows via a .bat file with the following settings:
start "MyServer1" java -classpath ./*;Server.jar infoServer/StartInfoServer
start "MyServer2" java -classpath ./*;Server.jar loginServer/StartLoginServer
start "MyServer3" java -classpath ./*;Server.jar chatServer/start
start "MyServer4" java -classpath ./*;Server.jar gameServer/start
However, when I move to trying to launch these classes from the .jar on Linux, I get a "could not find or load main class" error. My .sh file is set up like this, and is placed in the same directory as my .jar file:
echo Starting Servers
java -cp Server.jar infoServer.StartInfoServer
java -cp Server.jar loginServer.StartLoginServer
java -cp Server.jar chatServer.start
java -cp Server.jar gameServer.start
echo All Done Starting Server
I've used ls from the Terminal to verify the .jar and .sh were being recognized as existing where they should be. (For future note, I'm using the Terminal from inside the directory containing my files.) I've made sure to make use of chmod to be sure both the .jar and the .sh have read/write/execute permissions and used ls -l to verify those permissions were indeed present. I've tried various forms of explicitly defining the classpath, such as using "/home/machine/Desktop/Folder/MyJar.jar", using pwd from the Terminal to ensure I'm getting the filepath correct. I've checked over my Java compatibility. (1.7.0_65 on Linux, 1.8.0_45 on Windows, with the .jar being created in Eclipse using 1.7 Compliance settings.) I can use unzip MyJar.jar from the Terminal and it will properly extract all my class files, allowing me to verify that my .jar isn't corrupted on my Linux machine and that the paths to the classes I'm trying to run are still the same on Linux as they are on Windows.
I do apologize if this is just a problem of inexperience overlooking something, but I can't think of or find any indication of what the problem could possibly be.
EDIT:
I've updated the question with some screenshots related to the problem:
https://gyazo.com/0ae2a2701aae734db21ef7c29200283b - General File Setup.
https://gyazo.com/d735d9cee57b4a92078c4b624d012b8c - Running the Shell via Terminal.
Other notes: jar -tf Server.jar works from the Terminal but not from inside the Shell script, which leads me to believe this may be some kind of visibility or pathing error, but I can't find any reason why that would be the case.
I am scheduling a task (with windows task scheduler) which simply run a batch file.
this batch is running a jar file (Java program) with a simple "Java -jar test.jar".
When i run the script from the command line manually, the java program runs well and no exception is shown.
but when the task schedular does the same (calling the command), the java program ends with an exception: "ClassNotFoundException" for one of the classes which is in my jar.
What way be the cause of this? what is the diffrence when calling the jar from the command line and from the task scheduler?
Thanks.
I reckon that probably the "current directory" is different, and as a consequence java doesn't find the jar at all. In the first line of your .bat, can you do a cd \path\that\you\expect before you execute java?
Does your jar have any dependencies? Also, it'd be helpful to know what's your folder structure, and how exactly do you run it in the command line.
Anyways, depending on your case, you can do something along these lines:
cd /path/to/exec/folder // set current directory
java -cp /all-classpath-jars/and-or-bin-folders/ test.jar your.package.MainClass [args...]
This has to work, if you specify everything you need correctly.
In an application I am writing, I am launching another application (a runnable JAR) using Runtime.exec(...). Everything launches successfully in Windows, but Linux (specifically certain installations of CentOS - works in Ubuntu) has been giving me some problems. For some reason, the only way the secondary application will successfully launch is if I execute the first through a terminal. All behavior works as expected. However, if I launch the first application by double-clicking its icon (without a terminal open), the button to launch the second application seems to do nothing. I get no exceptions or error output - just a flash of my progress bar saying that it is launching, and then nothing. I can confirm through jconsole that the second application's process is never launched.
I have seen the commonly linked article on the pitfalls of the exec method ( http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html ), but have not been able to solve this problem with anything I have found there. I am in fact reading the output and error streams of the second process, as I see all output when it successfully runs (after launching the first application through a terminal command). Not knowing a lot about deeper workings of Linux, I think this sounds like it may be a permissions issue with the output stream or something, but I am not sure.
In case it helps to diagnose the problem, I am using the command:
rt.exec(new String[]{"\bin\bash", "-c", "java -jar myjarfile.jar myArg1 myArg2 ..."}); Since this works (depending on how the application is launched), I'm not too concerned that anything is wrong with this piece of code...
Anyone have any suggestions? Thanks in advance!
EDIT: The solution was to fix the directory to the JAR I was attempting to run. When launched via the GUI, user.dir was pointing to the parent directory of the folder containing my application. Since I'm using Eclipse RCP, my solution was to use
String currDirPath = Platform.getInstallLocation().getURL().toString(); instead. Thanks for the help everyone!
Since you're just using the jar file name - myjarfile.jar - and not the full path to it, depending on the current working directory, the jar may or may not be found. Try changing your exec command to use the full path to the jar instead. You can debug this by using rt.exec() to write the output of 'pwd' to a text file.
instead of
rt.exec(new String[]{"\bin\bash", "-c", "java -jar myjarfile.jar myArg1 myArg2 ..."});
use
rt.exec(new String[]{"\bin\bash", "-c", "/***path to java***/java -jar myjarfile.jar myArg1 myArg2 ..."});
My java program was written on a windows machine and I am trying to get it installed and running on a Ubuntu 10.04 machine. I have created a .tar.gz file with myProgram.jar in it as well as 5 supporting library .jar files in a lib folder. Where do I put these files? Do I need to extract it on the Linux machine to a usr/bin folder? Does the shell script go inside the tar.gz? I have read that if you write the shell script on a windows machine you can have issues once you move it to the Linux machine, so I am writing the shell script on the Linux machine using gedit. I am just not sure what to do next.
So far in my script I have,
#!/bin/bash
java -jar myProgram.jar
I am going to try and extract the tar.gz file to the usr/bin directory and see if it runs.
Any suggestions or help would be greatly appreciated.
Thanks in advance,
Ray
Your question is quite "broad" :). I hope you find the following useful.
Do not extract the files to /usr/bin. See e.g. http://en.wikipedia.org/wiki/Filesystem_Hierarchy_Standard on where and where not to put files on a *nix system.
Extract the jar's to e.g. /opt/yourProgram/*.
The shell script should be inside there too. Make sure its executable (i.e chmod 755 script.sh)
In your shell script add cd /opt/yourProgram to have the proper working directory for your program before you invoke java.
If you want this program to be started easily by everyone create a symbolic link in /usr/bin or better in /usr/local/bin pointing to your script. Do this as last step after everything else is working.
In your shell script you'll have to add the other jars to the classpath e.g.
java -cp lib/some.jar:lib/other.jar -jar myProgram.jar
or
java -cp lib/some.jar:lib/other.jar:myProgram.jar com.acme.ClassContainingMain
Recommended practice: Add set -e at the very beginning of your script
As you already mentioned it's considered harmful to edit a shell script using a windows editor. The reason is that the windows editor will encode line-breaks (i.e. you hit the Return key) differently. This will make bash puke :)
Im not too clear of what you are looking for.
The script that you have written should work absolutely fine if you have placed your script and myprogram.jar at the same level.
And also im not sure how your myprogram.jar is referring the dependent libraries. So can't comment on them. Best bet will be to place your script and all jars together and try running the script.