I'm looking at http://akuma.kohsuke.org/, but it's only for POSIX systems. I don't need daemonization, just an orderly way to launch more jvms from a java program. Yes, ProcessBuilder, but how do I find the right java.exe?
System property java.home contains path to current java home. For example on my computer it is C:\Program Files\Java\jdk1.6.0_21\jre.
So, the following statement System.getProperty("java.home") + "/bin/java" should give you the fully qualified path to your current JVM.
If you want to be even more cross platform use File.separator instead of slash.
Related
I've been struggling to find a reason why my Java application does not use the default PATH environment variable. I need it to launch another program with ProcessBuilder. Right now I get "Cannot run program "..." error=2, No such file or directory", though I can run this program from the terminal. I'm using JDK 1.8, Netbeans 8.1, OS X.
Here's the output of System.out.println(System.getenv("PATH"));:
/usr/bin:/bin:/usr/sbin:/sbin
The actual value of PATH in the terminal (using echo $PATH) is much longer and contains the paths to the desired executables.
I found some questions treating on the same subject but none could help me with this.
Any help appreciated!
PATH is created by whatever shell you're running, but ProcessBuilder does notrun within a shell and therefore there isn't a PATH to attach to, to resolve your program names. You can provide an environment to ProcessBuilder, but don't believe it's going to let you find your program that's in the PATH. In a project of mine I had to provide a fully-qualified path.]
[NOTE: Mileage may vary, I seem to remember having somewhat different results between Windows and *nix, and between different *xix.]
I have a bash script that bootstraps a Java process. I want it to be able to run on *nix and Cygwin. The problem is the separator in the -classpath parameter is different under the two platforms (: under *nix and ; under Windows).
I can't find an environment variable that specifies this separator (same as $PATH separator), so is there a better way to solve this than detecting the OS and hard-coding?
No. If you write your bootstrap code in Java rather than Bash, you can use the path.separator system property.
Otherwise, it's quite normal to see separate launcher scripts for both Unix and Windows.
Okay I need both java and C compilers to work on my pc.
So I have set the path variable of C and now I need to set java's path, so for that can I set the path variable of java in the same PATH (where I set path for C) or do I need to create a separate PATH for setting it up? Some one please help.
If I set both in a same path, then will anything go wrong? Sorry I am new to this, please bear with me. Thanks
It would matter if there were any name collisions. To my knowledge there's no such, but since there are many C compilers and much more stuff for them, and the most important java stuff starts with 'java', just add the Java path AFTER the gcc/mingw path
SET PATH=%PATH%;%MINGW_PATH%\bin;%JAVA_PATH%\bin
I am introducing you to what is PATH variable ?
PATH is an environment variable on Unix-like operating systems, DOS, OS/2, and Microsoft Windows, specifying a set of directories where executable programs are located. In general, each executing process or user session has its own PATH setting.
On POSIX and Unix-like operating systems, the $PATH variable is specified as a list of one or more directory names separated by colon (:) characters
On DOS, OS/2, and Windows operating systems, the %PATH% variable is specified as a list of one or more directory names separated by semicolon (;) characters.[3]
Note : The above is an excerpt from WIKI
For Windows, you can set the path
SET PATH=%PATH%;%MINGW_HOME%\bin;%JAVA_HOME%\bin
For Unix-like operating systems, you can do
SET PATH=%PATH%;\home\%MINGW_HOME%\bin;\home\%JAVA_HOME%\bin
Is there some way I can find the path to the java.exe that launched me?
In this case I am on Windows but if there is a general solution I would welcome it.
Added
FYI: In most cases my tool will be launched from a full path to an unregistered java.exe. e.g. C:\ABC\DEF\SDK\JRE\BIN\Java -jar MyTool.jar. Can I find the C:\ABC\DEF\SDK\JRE\BIN\.
Yes it is rather self-referential. :)
Generally speaking, there is no need for java.exe to launch java code. But you might want to look into the java.home system property
There's a java.home system property. I am sure that cases can be contrived where you start with JAVA_HOME set to a different directory from the one that contains your java.exe, but I this should be good enough for standard cases.
scala> System.getProperty("java.home")
res3: java.lang.String = C:\Program Files\Java\jdk1.6.0_14\jre
For windows when you execute java.exe from command line or from native OS it will look for all the dir listen in env variable PATH + the current directory. Hope that helps
I have Ubuntu 10.10 with java already installed. I am able to execute java command from any folder. I supposed that could be because I had java Classpath setted. But neither JAVA_HOME nor CLASSPATH are setted.
If I look at /etc/environment content I can see that PATH is setted to /usr/bin/ (among others). As 'which java' returns /usr/bin/java, is that the reason why I can execute java from anywhere? If not, why is it?
You can execute java because the command is on your path.
echo $PATH
will show you which directories are searched, in which order to find a particular program. Since /usr/bin is on your path, when you type java it will eventually look at /usr/bin/java. Note that in many systems this is a symbolic link (a file that points to another file) so /usr/bin/java often points to /etc/alternatives/java (which is also a symbolic link that points to the real executable).
Where the environmental variable JAVA_HOME comes into play is in tools and programs that check for JAVA_HOME and act on it instead of relying on the path. In most modern Linux systems, the work done by the alternatives subsystem replaces the earlier (more problematic) JAVA_HOME technique. That said, you might want to set JAVA_HOME anyway, should you encounter a tool that demands it.
One reason why JAVA_HOME is not as popular as it could be is that to access JAVA_HOME you need to run a shell, and not everyone wants to wrap every single Java item in a shell command.
Yes, if java binary (or a link to it) is on a folder that is listed on the path then you can execute java without specifying the path to it (for example /usr/local/java/latest/bin/java -jar x.jar)
JAVA_HOME and CLASSPATH have nothing to do with system path.
JAVA_HOME allow other software (or scripts) to know where to look for java installation.
CLASSPATH tells java where to look for classes (.class files resulting of compiling .java files).