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.]
Related
I recently installed Java (Java Runtime 1.8.121) to my machine. I need to set up JAVA_HOME and set the variable from the Java folder where it installed.
Everywhere I have looked online says the Java folder should be located in Program files (x86), however, it is nowhere to be found. Its not in program files (x86), not in program files.
I wasn't able to choose a destination when it was installed as Software center was used. It simply completed the installation.
Can someone please help me find where the Java folder is so I can set up JAVA_HOME???!
cmd:
where java
git bash (same as cmd or):
which java
powershell:
(get-command java.exe).Path
Superuser: Equivalent of cmd's "where" in powershell
Open a command prompt
type: wmic product where "Name like '%%Java%%'" get installlocation,Name
This command can take a minute to complete. But should return something like this.
Edit: The benefit of this command, is that it doesn't rely on any system environment variables. It searches for installed programs that have the word 'Java' in the name. It won't return extra files or locations.
I have my java working properly and also the environment variable is also set for java jdk 1.8.0_261
But when I run the pig -version its shows Error: Could not find or load main class C:\java\lib\tools.jar.
I have also checked the java folder in lib for tools.jar and it is there.
Also the Apache Pig configuration is also correct in pig.cmd
My Hadoop and MapReduce all are working fine with no error.
Your installation is broken, or you have messed up a shell script. That error cannot really mean anything else.
When invoking java.exe, you can either pass a jar file, via java -jar C:\foo.jar, or a classpath + fully qualified class name: java -cp C:\foo.jar;C:\bar.jar com.foo.fullyqualified.ClassName.
That error means you've passed C:\java\lib\tools.jar in the position of that classname. That will fail to run on any system and regardless of installations: C:\java\lib\tools.jar is a file path, not a fully qualified class name. That tools.jar exists in that location is immaterial: It's as if I told you: "I searched for the book titled "Can of Paint" on your bookshelf and I can't find it", and you going: "That's bizarre, because (pointing at the cans of paint on the shelf in the garage), there are cans of paint right here". Java isn't treating that as a path to look up; it is treating it as a class name to search for on the classpath.
Note that this trivial invocation:
java.exe C:\lib\tools.jar
produces the exact error you are observing and that is the correct behaviour of java (because the right way to run it is either java -jar C:\lib\tools.jar or more likely java -cp C:\lib\tools.jar;. com.foo.YourApp. The fix is to.. not invoke java incorrectly like this.
Reasons
Perhaps pig.cmd is broken. The image you pasted does not include the actual call to java.
Perhaps java is broken. Does java.exe work at all? Make some test file, compile it, run it. Does that work?
Perhaps the script is trying to start the jar by letting windows do it (so, double clicking it, or in a script, start foo.jar, which will then end up checking the HKLM registry section for .jar, and then checking the key there for how to start it, and if you've manually edited that, that would break things: You then presumably set it for e.g. java %1 which is broken (it'd have to be java -jar %1).
pig -version doesn't invoke the pig.cmd you think it invokes, but something else.
Something in pig.cmd starts another script. Then that script is broken, or isnt the script pig.cmd thought it was, etc.
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 am brand new at Java and have been trying to write "Hello World!". I have gotten far enough to install java with jdk and I've see countless videos on the environmental variables and setting those up with the paths. In the CMD when I write Java or Javac, it works fine, but trying to do step #4 (compiling) on this step by step site: http://www.codejava.net/java-core/how-to-write-compile-and-run-a-hello-world-java-program-for-beginners
I get completely stuck. Its when I am trying to "change the directory" that it tells me that the system cannot find the path.. Right now I'm at a complete loss.
Any suggestions on what I need to do? I am happy to post screen shots if that will assist as well.
Java error picture:
When running the command in the prompt window, Windows is looking at the PATH environment variable.
Make sure that the bin directory of the JDK is referenced in that variable.
When I execute a command using ProcessBuilder, how does it know where to look for that command? Using this hack/trick I've modified my PATH variable (verified by inspecting processBuilder.environment()) to be bad (empty, working dir, etc) but ProcessBuilder can still execute sort, echo, bash, etc. just fine. How is it doing this?!
Note: My particular development environment is OSX but this code will also run on Red Hat Enterprise Linux.
The documentation says
[...] a command, a list of strings which signifies the external program file to be invoked and its arguments, if any. Which string lists represent a valid operating system command is system-dependent. [...]
Which in essence mean that where it looks for programs to execute depends on the particular system and JVM you're running on.
I can't find a complete matrix of JVM / System behaviors, but supposedly it behaves similar to the popular shells of the system (bash for *nix and cmd for windows) i.e. it searches the directories in the PATH environment variable from left to right and executes the first executable file it finds.
If you want to take control of finding commands, then, well, take control of finding commands. Don't let ProcessBuilder search. Use your own code to find what you want to run, and then put an absolute pathname into the parameter to ProcessBuilder.