Java: How to run a Java process in a specific directory? - java

When I run a jar file, say in /home/jars like such java -jar /home/jars/jarfile.jar, it seems to run in whatever directory I'm in.
How do I make it so java -jar /home/jars/jarfile.jar runs with /home/jars as the current working directory??

The simple answer:
( cd /home/jars; java -jar /home/jars/jarfile.jar )
See also this response.
The analogy for most languages supporting an exec call, is the variant of exec that lets you specify a working directory.
See this for simple PHP solution.

Related

What does -cp mean in the terminal when running a .jar file?

I have to get some kinks out of a shell script for work, and one of the line looks like this:
-cp: this is the classpath
This is the set of classes that are used when running a specific class.
In your example; OrganT.Tune.Mix OrganT must be a class in the classpath (in this case, inside the OrganT.jar
Read the documentation, can be found here
Just a hint - under linux and mac you can use the
man <command goes here>
comman in the terminal/shell to display all parameters and usage information available for the specific command.
-cp stands for classpath. The CLASSPATH variable is one way to tell applications, including the JDK tools, where to look for user classes.
java -classpath .;YourJarFile.jar
I think you want to run a script for including the class path and execute the jar.
To do this in any text editor type java -jar YourJarFile.jar and save it, with extention (anyName.sh) assuming you have got linux flavour. Make it executable using the command chmod 775 anyName.sh
For windows type java -jar YourJarFile.jar, and save it with extention (anyName.bat)

Can you run a Java program in Linux using the name only, without the "java" command?

If I am writing HelloWorld, is there a way I can run the program from any directory by just typing HelloWorld? Sort of the same way once you set up Ant, you can just run Ant from any directory?
Just for some details, we are creating a CLI based toolkit for a customer, and just curious if we can compile it, install it, and just have them run it using the toolkit name.
You can always create a shell script, call it HelloWorld and make it run java with your JAR.
You'll then need to chmod the script to make it executable, and place it somewhere in your $PATH.
The script would like something like:
#!/bin/bash
cd /path/to/helloworld
java -jar HelloWorld.jar "$#"
or
#!/bin/bash
java -jar /path/to/helloworld/HelloWorld.jar "$#"
depending on your exact requirements.
Common solution for your problem is to create a separate launcher application, which is non-java application that runs your Java program. Launcher can be written in some compilable language such as C/C++ and compiled into native executable. Also it can be written in some interpreted language such as Unix shell, perl, python etc and made executable by adding #!/path/to/interpreter line at the beginning of launcher file and setting executable flag on it. Also there are several utilities that can generate launcher for your program such as launch4j or jsmooth.
On Linux (specifically), you could use the /proc filesystem (see proc(5) man page) and its binfmt_misc (actually the /proc/sys/fs/binfmt_misc/register pseudo-file and other pseudofiles under /proc/sys/fs/binfmt_misc/) to register java as the handler for .class or .jar files. Read the Documentation/binfmt_misc.txt file in the kernel source for gory details.
Then any executable .jar file would be interpreted by java (or jexec)
I'm not sure it is worth the effort. I find that wrapping your Java program in some shell script is much more easy (and more portable, because few Linux systems actually use binfmt_misc, and your customer may need some sysadmin skills to enable it).

Run java class from single command using makefile

I have a homework assignment in Java that is tested using the commands:
make
./<program_name> <arguments>
my make file compiles my java program successfully, but how can the program be run without using the command:
java <program_name>
I have investigated how to convert a .jar into an .exe but I am convinced that is not the answer I am looking for.
I believe the test is run on a Linux machine. Is there something I can include in the make file to cause the command
./<program_name>
to run a compiled java class?
Without converting the java program in a native executable file, that will be different for linux, for windows and any other platform (so you will loose Java portability), the only thing you can do is to create a launch script.
On *nix system you can create a bash script and on windows a batch script. Then in this script you have to call java <program_name>.
With the script you are now able to launch your application with a single command.
For example on unix you can create myapp.sh:
#!/bin/bash
java -classpath bin com.test.YourApp $*
and make this script runnable with command
chmod a+x myapp.sh
in this example when you write myapp.sh command you launch your Java class com.test.YourApp using the folder bin as classpath.

Deploying a Java project on Linux system

I developed a project using Java and now I've to deliver it to client who is using Linux. Which executable file format will be delivered and how to make that?
Executable file format?
If you're delivering a Java app, give them a jar file (and associated libs).
Provide a shell script to set up its environment and execute it.
For example, assuming I define ROOT_DIR as my app's install directory, and so on:
CLASSPATH="${ADD_JARS}:${CLASSPATH}:${ROOT_DIR}/lib/myApp.jar:\
${ROOT_DIR}/lib/jibx/jibx-run.jar:\
${ROOT_DIR}/lib/jibx/xpp3.jar:\
${ROOT_DIR}/lib/bindings.jar:\
${ROOT_DIR}/lib/commons-lang-2.0.jar:\
${ROOT_DIR}/lib/forms-1.0.5.jar"
"${JAVACMD}" -Xmx256M -DanyDefsNeeded=foobar -Dbase.dir="${ROOT_DIR}" -cp "${CLASSPATH}" myApp.main.Launcher "$#"
What goes into the shell script depends totally on what your app actually needs to start up.
A jar. If it is not executable, then a script (.sh) to launch the jar.
Well basically what you wanna put in a .sh file is the commands you'd normally type at the console to run your jar file. They should be separated by a new line (i.e. each on a separate line in the .sh file).
The most basic you can go is add something like this to your sh file:
java -Xms=64m -Xmx=256m -jar myJar.jar -classpath [dependencies dir]/dep1.jar,[dependencies dir]/dep2.jar
beyond this you can do more exotic stuff, like parametrise some environment variables, get command line argumens from when the .sh is launched and pass them to the jar executatble etc. Look up "bash scripting" for advanced stuff:
http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-2.html#ss2.1
You might have better luck using Launch4J, IzPack or other installer that has cross-platform capabilities. This might be a better first option than trying to understand the intricacies and idiosyncrasies of the different Linux distributions and shells.
If your app. has a GUI, the best user experience for installation/deployment can be had by way of Java Web Start. Note that JWS can deploy apps. to Windows, *nix and Mac. and avoids all the maintenance woes of generating 3 separate (platform specific) executables.

Calling java from PHP exec

I am doing the following in PHP:
exec('java -jar "/opt/flex3/lib/mxmlc.jar" +flexlib "/opt/flex3/frameworks" MyAS3App.as -default-size 360 280 -output MyAS3App.swf');
When I run this from the command line, it runs fine and finishes in a second or two.
When I run this command from PHP exec, the java process takes 100% CPU and never returns.
Any ideas?
I have also tried running the above command with '/usr/bin/java -Djava.awt.headless=true'.
I am running Mac OS X 10.5.5, MAMP 1.7, PHP 5.2.5
Turns out it was a bug specific to the PHP stack MAMP (http://www.mamp.info/).
Turns out any invocation of the JVM following fails under MAMP, e.g.:
exec('java -version');
The fix is to prefix the command with
export DYLD_LIBRARY_PATH="";
Also I realized there's no reason to use that method of invoking mxmlc.
So here's the final, working command:
exec('export DYLD_LIBRARY_PATH=""; mxmlc MyAS3App.as -default-size 360 280 -output MyAS3App.swf');
I manage to get this to work togheter with MAMP. The solution was to include the:
export DYLD_LIBRARY_PATH="";
in the exec call:
$argss = "export DYLD_LIBRARY_PATH=\"\"; /usr/bin/java -jar /Applications/yourjarfile.jar";
$resultXML = exec($argss, $output);
Is there a reason why your using the mxmlc jar file to compile your flex application? have you tried using the executable or an ant task, instead?
Maybe the compiling is taking too long so that your PHP script times out?
Exec is always tricky, on any language :-)
Try to:
use background execution (add &
symbol at the end)
use shell_exec instead
specify the full path to
java executable (may be the one
available to PHP is not the one you
need?)
run a simple HelloWorld java
app to see if the problem is in Java or
in mxmlc specifically
It's strange that java takes 100% CPU. I cannot explain it with any common mistake made when using exec()... try to send it a SIGQUIT to dump the threads, then read the dump -- may be you'll figure something out.

Categories