I built a java program that runs the command "jps" and sees all JVMs and kills a particular JVM by extracting its id from the output of JPS command. It is working fine when I run it on the ubuntu terminal. But then I wrote a script in bash to ssh that machine from other machine and run this program there.
ssh $host "java -cp daemon.jar JVMname;"
Now here comes the problem.
Exception in thread "main" java.io.IOException: Cannot run program "jps": error=2, No such file or directory at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029) at runtime.daemon.halt.main(halt.java:19)
Caused by: java.io.IOException: error=2, No such file or directory at java.lang.UNIXProcess.forkAndExec(Native Method) at java.lang.UNIXProcess.<init>(UNIXProcess.java:135)
at java.lang.ProcessImpl.start(ProcessImpl.java:130)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1021)
If I go to the the machine myself and run this same command it works. I do not want to switch to exec solution.
Any ideas
Thanks
This isn't a Java issue per se, it's down to the difference in your environment in both cases. Specifically, when you run interactively, your $PATH variable contains the directory for jps, whereas in the latter case via SSH it doesn't.
Have a look at the .bash_profile and .bashrc files on the remote machine - I suspect the path will be set in the .bash_profile file, which isn't executed for non-login shells (such as your SSH invocation that runs a single command). If you set the path correctly in .bashrc, then your current invocation should start working.
(Note this assumes you're using bash for a shell, though most other shells have a similar distinction between the login shell and non-login shell init files.)
Related
I'm running into trouble trying to compile the eXo platform.
The error message is:
Caused by: java.io.IOException: Cannot run program "/exo/platform-public-distributions-develop/plf-community-tomcat-standalone/target/platform-community-5.0.x-SNAPSHOT/platform-community-5.0.x-SNAPSHOT/addon" (in directory "/exo/platform-public-distributions-develop/plf-community-tomcat-standalone"): error=2, No such file or directory
The directory is correct, the file indeed does exist, and permission is executable:
-rwxr-xr-x 1 root wheel 3379 Sep 3 12:21 addon
That "addon" is a bash script.
I think the error mssage "No such file or directory" is erroneously reported. I saw this answer, but it is telling us to change the source code. Is there anyway to set some path for Java to execute the shell script?
The bash script probably has a shebang (#!/bin/sh) at the top. This is normally interpreted by bash when invoking the script. However, running from java is not going to do this automatically. You will need to explicitly invoke bash to run the script.
You didn't post your code, but you need to set bash (or /bin/bash) as the executable and make the script be the first argument to bash. This will explicitly invoke bash to run the script. You need to execute:
/bin/bash /exo/platform-public-distributions-develop/plf-community-tomcat-standalone/target/platform-community-5.0.x-SNAPSHOT/platform-community-5.0.x-SNAPSHOT/addon
I downloaded SymmetricDS, a tool for Database replication and tried to run it on my Windows7 machine. The program can be launched from command line and it works with Windows Terminal. However I always prefer Git Bash for command line stuff. When I run command sym though, I got error:
Error: Could not find or load main class org.jumpmind.symmetric.SymmetricLauncher
This tool is written in Java. I have JDK 1.8 installed. Git Bash inherits all environmental variables including $PATH and $JAVA_HOME from Windows. But why is it complaining about not finding the class?
The sym command is really running the following command:
exec "$SYM_JAVA" $SYM_OPTIONS -cp "$CLASSPATH" org.jumpmind.symmetric.SymmetricLauncher "$#"
All the jars are located in lib under the root directory of the application. The classpath is defined in a sym.service.conf inside conf directory:
# Java Classpath
wrapper.java.classpath.1=patches
wrapper.java.classpath.2=patches/*
wrapper.java.classpath.3=lib/*.jar
wrapper.java.classpath.4=web/WEB-INF/lib/*.jar
# Application main class and arguments
wrapper.app.parameter.1=org.jumpmind.symmetric.SymmetricLauncher
I added echo $CLASSPATH right before the exec to print out the class path and it did seem to get all of them right:
/c/Users/dnj0109/Apps/symmetric-server-3.8.29/patches:
/c/Users/dnj0109/Apps/symmetric-server-3.8.29/patches/*:
/c/Users/dnj0109/Apps/symmetric-server-3.8.29/lib/*:
/c/Users/dnj0109/Apps/symmetric-server-3.8.29/web/WEB-INF/lib/*
That could be related to this thread:
On Windows, the path separator is a semicolon (';' instead of ':').
Don't ask why. Traditionally, the semicolon is interpreted by the Bash as
the command separator, so you'll have to escape it:
$ java -cp lib/clojure-1.1.0.jar\;lib/clojure-contrib-1.1.0.jar
If you wonder why it works with PATH: MSys has special handling routines
for that.
Another reason that a java app may run in a Windows CMD shell but not in a Windows git bash shell is that the classpath used to run the app contains one of the following:
relative paths (e.g. ../foo)
network drives (e.g. //servername/bah
See:
https://github.com/git-for-windows/git/issues/1028
I want to try to e2fsck on Windows with Cygwin exe and dll files.
Fortunately, it works fine when I try to execute it with Windows command line(such as "e2fsck.exe -f test.img").
However, I failed to execute it with JAVA API, Runtime.getRuntime().exec("e2fsck.exe -f test.img").
Exit value of process is 8, and it means operational error of e2fsck. The exit value seems to be returned when e2fsck can not access target file, or target file is invalid.
I found the below things :
It's not caused by diffrence of string handling way of cmd.exe and exec().
Any other cmds on API call, such as ...exec("echo test! >> test.img"), work fine.
It means that exec() API put the right file path to e2fsck or any other processes.
It's not caused by permission deny. As I mented above, test.img is writable by exec().
ProcessBuilder could not solve this problem.
I couldn't find actual reason of my problem.
Did I miss somthing else?
I'm using Jenkins to deploy the build.
We need to extract files from a .war into some directory.
We have an .xml file which includes commands to extract the files from the .war file and start the server.
The build is being created properly but the .war extraction is not happening and the destination folder is being left empty. The Jenkins Console shows the following output:
[sshexec] bash: jar: command not found
As read from other answers, I have set the PATH properly in .bashrc or in .profile but I'm still facing the same issue.
Install Java devel as follows:
yum install java-devel
To be specific, JAVA bin directory is not in your PATH variable. Add it to PATH variable. In order to search the executables, the OS need to have a list of directories to look up. So, Add the directory which contains 'jartool in thePATH` environment variable
Note : For Windows, Path separator is (;) and for Unix-like OS, Path separator is (:)
The "[sshexec]" tells me you are using some Jenkins plugin to execute a command on a remote host.
This usually means Jenkins executes this (or an equivalent thing by implementing the SSH protocol natively):
ssh user#remotehost 'jar arg1 arg2...'
What happens here is the sshd daemon on the remote host will execute the default shell of the user and ask the shell to execute the command line. The shell is typically not executed as a "login shell", meaning the shell will skip the initialization steps that login shells normally do when the user logs in interactively.
You can study man (name of your shell) to see exactly what the difference is between a shell executed with -l option and one executed without it. In the case of bash it essentially means .profile and .bashrc will not be executed, so any PATH modifications you might have there will not take place.
I think your best bet is to provide the full path to the jar command when you want to execute it.
As you are executing the command on remote host so, it will invoke the new Shell on the remote host. The PATH which you have set in the current host .bashrc file will not make any difference. Instead if you set the PATH for Java in remote host's .bashrc things should work else if you are running any script then mention the complete path for JAR like /usr/jdk64/jdk1.8.0_40/bin/jar -tf /tmp/jars/abc.jar while invoking the command on remote host.
Two ways to do:
export PATH=$PATH:/usr/jdk64/jdk1.8.0_40/bin ---> in remote host's .bashrc
/usr/jdk64/jdk1.8.0_40/bin/jar -tf /tmp/jars/abc.jar --> invoke the jar command with complete path for JAR from remote host.
I have a unix machine running Jenkins master, and I have also configured a windows slave. That works nice. But when I run a groovy script using the groovy plugin (in option system groovy script), and use a java.lang.ProcessBuilder to run a maven command, I get this:
Caused by: java.io.IOException: java.io.IOException: error=2,
No such file or directory
at java.lang.UNIXProcess.<init>(UNIXProcess.java:148)
at java.lang.ProcessImpl.start(ProcessImpl.java:65)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:453)
... 122 more
Build step 'Groovy Postbuild' marked build as failure
Finished: FAILURE
And the directory which the exception says doesn't exist, actually exists.
So my question is how can I start a maven command using groovy script on a windows slave when the master is running on a unix slave?
I find it speculative why I get the exception java.lang.UNIXProcess when I am running the groovy script on windows (the script file itself is located on the same machine as the windows slave, and I also checked from System.getEnv() that JAVA_HOME is correct)