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
Related
I installed JDK on windows 10, 64bit, followed the documentation instructions.. CMD does not recognize javadoc command. What I tried:
-copied the path of the "bin" folder from Java and in cmd I wrote the command:
set path = "full_path_to_java_sdk_bin_folder"
I saw this on youtube and it worked for the guy, my cmd still did not recognize javadoc command -I set the PATH in system variables from control panel->system->advanced->environment variables and made sure that there are is no other bin folder...
Didn't find any other tips online...
The usual approach for Java development is to set a JAVA_HOME environment variable, and use that to update the PATH (for one thing, it makes it much easier to support multiple versions of Java). Also of note is that Windows puts the quotes oddly on the command line (oddly compared to every other platform that is) and if your path contains spaces you need to quote it correctly. Like,
set "JAVA_HOME=<full_path_to_jdk>"
set "PATH=%JAVA_HOME%\bin;%PATH%"
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 want to use elastic search with my PHP application.
after running service install command , I am getting fallowing error.
C:\elasticsearch-0.90.10\bin>service install
JAVA_HOME points to an invalid Java installation (no java.exe found in "C:\Progr
am Files (x86)\Java\jdk1.7.0_25\bin"). Existing...
Open up and Look into the service.bat file:
It searches for:
%JAVA_HOME%\bin\java.exe
hence your %JAVA_HOME% should not include bin in it.
A workaround if you do not have privileges to set up the environmental variables:
open the service.bat file,
a) Remove the line:
if NOT DEFINED JAVA_HOME goto err
b) Replace %JAVA_HOME% with your java jdk path, something like: C:\Program Files\Java\jdk1.7.0_51
Note: This is just a workaround which works all the time.
from your error message we can see your JAVA_HOME points to
C:\Program Files (x86)\Java\jdk1.7.0_25\bin
while it should point to
C:\Program Files (x86)\Java\jdk1.7.0_25
I think the error message is fairly descriptive , you should look in this folder
is java.exe there ? If not then your JAVA_HOME environment variable to point to the correct installation
heres a page showing how to do that
https://confluence.atlassian.com/display/DOC/Setting+the+JAVA_HOME+Variable+in+Windows
1st hit on google.
Either JAVA_HOME points to the wrong location or you don't have Java installed. Check and see if there's an install of Java in the location specified.
Also note that the JAVA_HOME variable should point to the root of the Java install not the /bin folder. This is being added by the program you're executing.
To change the JAVA_HOME variable to test further use the following in the command prompt:
set JAVA_HOME=c:\xxxx
Once you've established the correct value to use this can be permanently changed in Control Panel>>System>>Advanced System Settings>>Advanced>>Environment Variables
set JAVA_HOME=C:\Program Files (x86)\Java
Above must fix the issue, I came across the same issue while installing the elastic search.
i gave a try by giving "bin" part of path but it did not work, so i just give the top folder "java" in path, it worked.
Just to add my own experience.
i was setting JAVA_HOME using tab autocompletion.
It ends up something like "C:\Program files...:" and it didn't work saying that it cannot find the java program. I solved typing the JAVA_HOME path in the set command without using tab completion. The problem was probably related to the " in the env variable
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).
I have JDK installed in Vista. I want to run java from the command line. How do I configure the path variable?
If I'm not able to set the JAVA_HOME variable, what is an alternative?
Open My Conputer -> General Settings -> Environment variables and add as global
Key:JAVA_HOME
Value:C:\jsdk etc. etc.
And then modify the Path variable
Value ( type at the end )
....something;something;something;%JAVA_HOME%\bin
Reopen your command prompt and type
javac
And see if it works
EDIT
Moved my comment to the answer
The command prompt has some problems dealing with the empty space.
What I usually do is install the jdk directly on C: to get rid of space in "Program Files"
So installing it directly in C: will give you an "spaceless" path to the executable:
C:\jdk1.6.0_14\bin\javac
Instead of
C:\Program Files\Java\jdk1.6.0_14\bin\javac
Some times you'll get: C:\Program "executable not found" or something like that.
Installing directly on C: does the trick
I'd suggest looking at the java tutorial.
http://java.sun.com/docs/books/tutorial/essential/environment/paths.html
CLASSPATH manual
Running java from the command line
If the java program is available in your path then you should just be able to do;
c:> java -classpath somedir com.example.Main
if java isn't available to you then you can do
c:> SET JAVA_HOME=c:\path\to\java
c:> <same java stuff from above>
Use the -classpath option for the java command to specify where your classes are.
The value of the classpath option is a list of directories, JAR archives, and ZIP archives that contain classes, structured into directories that mirror the classes' package structure. The list uses the path delimiter, which is plaform specific: on Windows, it is ";", other platforms use ":".
Using the CLASSPATH environment variable is discouraged because changes for one program are likely to interfere with the operation of other programs.
There is some problem with Vista and the path variable containing %JAVA_PATH% if the JAVA_PATH value has spaces.
I fixed this manually by setting the %JAVA_PATH% to c:\program files\Java\<whatever> and then added to c:\program files\Java\<whatever>\bin to the Path variable in Vista.
If you deleted your My Computer icon, you can also go to Control Panel -> System -> Advanced system settings (which gives you System Properties dialog) -> Advanced tab -> Environment Variables. Path is listed in the second text window.