so I have the following script
$execStr = 'cd /D Z:\\folder'
exec($execStr);
$execStr = 'java -jar somejar.jar';
exec($execStr);
the jar does some operations on a mysql database...
but when I run it, it appears that the jar did not run properly because the database was unchanged
but then when I run the exact same strings from the command line it would work properly
what am I doing wrong?
shell_exec('/path/to/java -jar Z:\folder\somejar.jar');
where you sub /path/to to the real place your java is run from.
Try this:
$execStr = 'java -jar Z:\folder\somejar.jar';
shell_exec($execStr);
Try running dir or ls after your cd to make sure the exec environment is persisting from one command to the next.
I guess that's because when you call java -jar ... you're no longer in Z:\folder directory. Try a single command:
java -jar "Z:\folder\somejar.jar"
Related
I have created a jar file called test.jar under C:\jars. I have JAR file under the same location named run.bat and it contains the below code -
#echo off
set exec_path=C:\jars java -cp %exec_path%/test.jar; com.mycomp.myapp.MyProgram "%1"%*
#echo on
It is running successfully from command prompt with parameters.
Now I would like to run it from another JAVA program.
Please suggest.
Thanks!
I've encountered this issue before. The answer is that you have to run cmd.exe or bash or whatever shell you've got, then feed in the command to that process via the process input/output streams.
Process p = Runtime.getRuntime().exec("cmd");
p.getOutputStream().write("mybatch.bat\n");
I need to launch a Jar file with a portable JRE. It works on Windows thanks to launch4j, it works on Ubuntu thanks to me, but not on Mac ..... and I can't test it.
The path tree of application:
abcmap/
launcher.command
bin/
abcmap.jar
jre/
bin/
java
Thinking it's like Unix system, I tried this (without specify bash):
./bin/jre/bin/java -Xms512m -Xmx1024m -jar ./bin/abcmap.jar
But this error appear:
MacBook-Pro:~ frcstnt$ /Applications/abcmap-mac/abcmap.command;
exit;
/Applications/abcmap-mac/abcmap.command: line 1:
bin/jre/bin/java: No such file or directory
logout
All files are rwx for all,
All files are UTF8 without BOM
I tried to change end of line to Unix and to "Old Mac" with Notepad++,
Archives are available on my website:
http://abc-map.fr/download/
https://translate.google.fr/translate?hl=fr&sl=fr&tl=en&u=http%3A%2F%2Fabc-map.fr%2F
Someone can help me please?
Edit: I think my problem is in the launcher, maybe encoding or bad command ?
Edit: Thanks to the Wim's answer it works. This is the script:
#!/bin/bash
SCRIPTDIR=$(dirname $0)
cd $SCRIPTDIR
${SCRIPTDIR}/bin/jre/bin/java -jar ${SCRIPTDIR}/bin/abcmap.jar
The path ./bin/jre/bin/java will be resolved relative to the current directory, not relative to the location of the command file. Try what happens if you run the command from the directory where the command is located.
I think I've found thanks to Wim Deblauwe. I use this:
#!/bin/bash
SCRIPTDIR=$(dirname $0)
cd $SCRIPTDIR
${SCRIPTDIR}/bin/jre/bin/java -jar ${SCRIPTDIR}/bin/abcmap.jar
Find the script dir, go to this dir and start operations.
It seems like you maybe don't even have the Java Runtime Evironment installed. You should try running java -version or at least java -h or java --help.
If you output is something like the following:
java: command not found
You should download and install the latest JRE and try again.
If, however, you see your java version, try first using cd to go to the directory of your jarfile and then use:
java -jar -otheroptionshere jarfile.jar
I hope this helps! Tell me, if you still got issues.
I have to create a Unix shell script to execute Java code which takes a input file and creates an output file. This is how I do it on Windows:
C:\Work\MCDExcelParserJE\bin>java -classpath .\;.\jxl.jar medicaid.Test PROCESS
How can I run this on Unix?
First thing is that you need to use colon(:) instead of semi-colon(;) in your classapth:
java -classpath .\;.\jxl.jar medicaid.Test PROCESS
should be updated to
java -classpath ./:./jxl.jar medicaid.Test PROCESS
Make sure you have jdk/bin directory is added to path in order to run the java commands.
I try to run an jar from an php file like this:
exec("java -jar /home/florinbuda/NetBeansProjects/monkey1/dist/monkey1.jar", $result);
print_r($result);
and IT WORKS if I run it from command line like:
$ php runner.php
but it doesn't work if I try to load it via http-localhost-server/runner.php the page just keeps loading forever without giving any error..
In the .jar file I added a simple test to now if the jar is even started to work
public static void main(String[] args) {
new File("/home/florinbuda/Desktop/xxx").mkdir();
and as you can guess - when I call http-localhost-server/runner.php the jar is not even started to be executed...
It's a problem of rights? What suggestions do you have?
Yes, it's the problem of permission.
You should chmod the parent folder of .jar file.
chmod 755 -R /home/florinbuda/NetBeansProjects/monkey1/dist/
EDIT: And then your script should look like this:
exec("PATH_TO/java -jar /home/florinbuda/NetBeansProjects/monkey1/dist/monkey1.jar");
The environment variables in bash aren't the same in exec(). Use the full path for java bin at least. You could also try just to run /path/to/java to see the normal output is working.
I have a java program that I would like to be able to run from anywhere on my machine. I would like to run it from my Cygwin command prompt. I've made scripts to call the java program. I added the location of the java program to the classpath, and the scripts work when I run them from the java program's directory. However, when I try to run from any other directory, I get:
java.lang.NoClassDefFoundError: commandprogram/CommandProgram
This is my script:
#!/bin/sh
CWD=`dirname "$0"`
java -cp "$CWD/classes;$CWD/lib/AJarFile.jar" commandprogram/CommandProgram
Changing the java line to the following:
java -cp "$CWD/classes;$CWD/classes/commandprogram;$CWD/lib/AJarFile.jar" CommandProgram
produces the same results.
add your directory to classpath example:
java -classpath commandprogram CommandProgram
or
java -classpath directory_to_program Program
After trying just about everything I could think of, I echoed out the command and saw that there was mixing of Cygwin paths and Windows paths. The solution was to change the script to:
#!/bin/sh
CWD=`dirname "$0"`
CWD=`cygpath -w "$CWD"`
java -cp "$CWD/classes;$CWD/lib/AJarFile.jar" commandprogram/CommandProgram
Then CWD changed to "C:\Program Files\..." instead of "/cygdrive/c/Program\ Files/..."
I had previously encountered this problem and solved it with the cygpath -w solution, but then changed my script slightly and didn't notice that the path problem came back.
you have to use a dot to separate packages, not a slash.
java -cp "$CWD/classes;$CWD/lib/AJarFile.jar" commandprogram.CommandProgram
The usual way of running a java file is to save it in the Java/Bin folder and Run cmd
C:\Program Files\Java\jdk1.7.0_05\bin> javac filename.java && java classname
If you save the file in different directory such as D:, you can use the following on the cmd prompt:
D:\Project java> set path=%path%;C:Program Files\Java\jdk1.7.0_05\bin