Java:System cannot execute the specified program - java

The javac -version gives the proper version of javac.
But, java -version gives the error as
"System cannot execute the specified program"
The PATH variable is set properly as "C:\Program Files\Java\jdk1.8.0_102\bin"

It is not programming question. But you have to add your %JRE_HOME%\bin to PATH variable

That specific error is likely related to the evaluation of a symlink to the java executables.
Normally, an invalid executable name (meaning, one that is not found in the path) will yield an error with the following text:
'javax' is not recognized as an internal or external command, operable program or batch file.
As you are receiving a different error, this is likely not your issue.
When java is installed on a Windows PC, the installer creates a directory (usually with the path of C:\ProgramData\Oracle\Java\javapath, and places 3 symlinks in it. They are for java.exe, javaw.exe, and javaws.exe. This prevents your path from becoming too cluttered with potentially colliding executable names.
javac.exe (and keytool, etc) work because you added your JRE/JDK bin folder to your PATH. But why does doesn't java/javaw/javaws work? Because it finds the javapath directory above first (PATH is searched left to right). You can see this issue by using the where command. For example:
C:\>where java
C:\ProgramData\Oracle\Java\javapath\java.exe
C:\Program Files (x86)\Java\jre1.8.0_31\bin\java.exe
C:\>where keytool
C:\Program Files (x86)\Java\jre1.8.0_31\bin\keytool.exe
This would be an alright situation, except that the javapath\java.exe symlink is not working, a policy is disallowing its execution, or its path is wrong.
From an administrative command prompt, run the following command. The result should be similar to below
C:\>fsutil behavior query SymlinkEvaluation
Local to local symbolic links are enabled.
Local to remote symbolic links are disabled.
Remote to local symbolic links are disabled.
Remote to remote symbolic links are disabled.
If "Local to local" symbolic links are disabled, that is your problem (as this settings prevents the following reparse points)
You can enable them with the following command:
fsutil behavior set SymlinkEvaluation L2L:1
Double check that the setting changed with the query subcommand. You do not have to restart for this setting to take effect.
If that does not resolve your issue, ensure that the destination of the symlink exists. Try running it via Explorer; a console dialog should pop up then close. If it does not, an easy way to confirm it's correct is to right click the symlink and select "Open File Location". If a error dialog appears stating 'Problem with Shortcut: The item 'java.exe' that this shortcut refers to has been changed or moved [...]', then the path is incorrect.
You can see what the path is set to via the 'Shortcut' tab in the properties of the symlink. If the path is wrong, you can delete the symlink and then recreate it via the mklink command. Delete the symlink and then from an administrative command prompt, run the following; be sure to replace the second path with that of your actual JRE/JDK!
mklink C:\ProgramData\Oracle\Java\javapath\java.exe "C:\Program Files (x86)\Java\jre1.8.0_91\bin\java.exe"
At that point, run 'java' from the command line and see if it works.
If it does not, navigate to your desired java.exe, view its file properties, and ensure there is no "Unblock" button available on the bottom. If there is, click it.

you need to set the PATH variable(jre/bin/) in environment settings.
in cmd , set PATH ="C:/ProgramFiles/java/jre*/bin"

Related

Javac working with Powershell but not Command Prompt

I am trying to install Java in Windows 10 Home (64 bit). Like many tutorials point out, I downloaded the Java jdk setup file, executed it and once installed changed the Path in the Environment Variables. But for some reason, when I execute the command 'javac' it returns the not internal/external command error in Command Prompt but runs absolutely fine in Powershell. I tried to rectify this by uninstalling Java and reinstalling it very carefully, following all the steps but still this keeps on happening.
Also, during installation of Java, I lost the original value to my Path variable. Could this be causing the problem?
Edit : I was able to get the java command running in the command prompt but javac still won't work. I've already tried the methods suggested on other forums and this one but no luck so far.
See there is no problem with Java installation if you can find JDK(Java development kit) and JRE(Server Java runtime environment) in programme files in windows in a JAVA folder.
The main problem is with the environment variable declaration.
Go to environment variables and make a new path for that.
JAVA_HOME = "YOUR JDK & JRE INSTALLATION PATH"
And another one is
JAVA = "YOUR JDK & JRE INSTALLATION PATH"
Hope that help to you. After doing that if still there problem persisist just restart your PC and check again.
Just add your variable location up to /bin/
Refer to this :
https://www.windows-commandline.com/javac-not-recognized-internal-external-command/
Open file explorer
Right click computer then click properties
click advanced system settings
click environmental variables
click path (for me there was one in user variables for admin and another in system variables)
find the directory for javac (for me it was here C:\Program Files (x86)\Java\jdk1.8.0_191\bin)
so go back to file explorer and open program files x86
then open the java folder
then open the jdk folder
then open the bin folder
highlight the path in the search bar and copy it
click edit on path and paste into the path in the environmental variable for both(if you have more than one path)
If no path exists then create one in user variables
If there is any existing text place a semicolon ; between the text you pasted in there and the preexisting text.
close command prompt and reopen it.
type javac -version
the version info should be listed
This problem comes up with the directory of your command prompt.
check your PowerShell and command prompt default directory.
We can run command prompt as WinKey+R then type cmd, Hit the enter button then check our java version cmd javac -version.

On Windows 8.1 "javac -version" works but Cordova doesn't recognize it

I'm trying to set up Cordova. When I run cordova build android I receive the following error:
(node:6816) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Failed to run "javac -version", make sure that you have a JDK installed.
You can get it from: http://www.oracle.com/technetwork/java/javase/downloads.
Your JAVA_HOME is invalid: C:\Program Files\Java\jdk1.8.0_161;C:\Program Files\Java\jdk1.8.0_161\bin
However I can run javac -version just fine.
C:\>javac -version
javac 1.8.0_161
I've tried to set JAVA_HOME in various ways: pointing to the root; pointing to the bin folder; set it as a User variable and also a System one; add %JAVA_HOME%\bin to Path (both for User and System) and so on.
I've checked several articles about this issue, and they say that if I'm able to use javac -version then that's supposed to indicate that my environmental variables are set correctly. Even if that's the case, Cordova still doesn't seem to recognize it.
Update
Following Stephen C's instructions, I've reset my variables, yet my error still persists. As you can see in the image I can call javac just fine, and both JAVA_HOME and Path seem to be set in the right way. (Note that actually it is %JAVA_HOME%\bin and not C:\Program Files\Java\jdk1.8.0_161\bin in the Path.)
I think you are confusing JAVA_HOME, PATH and CLASSPATH
The JAVA_HOME variable should be set to the name of a single directory. Not multiple directories with separators. The JAVA_HOME directory is the top directory of your JDK or JRE installation. Not the "bin" directory.
The PATH and CLASSPATH variables consist of a list of paths. For PATH the paths are pathnames for directories. For CLASSPATH the paths are pathnames for directories or JAR / ZIP files (or a particular kind of wildcard).
The PATH is where the shell searches for commands if you use a command name that is a simple name; e.g. java or javac.
`
The CLASSPATH is one of the ways that you can tell Java tools to look cor compiled classes to load, compile against, etcetera.
Your error message says this:
Your JAVA_HOME is invalid:
C:\Program Files\Java\jdk1.8.0_161;C:\Program Files\Java\jdk1.8.0_161\bin
This is wrong for two reasons:
JAVA_HOME should not be a path
C:\Program Files\Java\jdk1.8.0_161\bin is not a Java home. The Java home is C:\Program Files\Java\jdk1.8.0_161 .... probably.
The other thing you may have gotten wrong is that changes to environment variables do not necessarily propagate. For example, if you start a shell and then change an environment variable via the Windows GUI, the change may not propagate to the shell. You may need to exit the shell and restart it to pick up the new value.
If you are unsure, you can run (for example) echo %PATH% to see the current value of PATH in the current shell or script.
UPDATE
According to http://cordova.apache.org/docs/en/7.x/guide/platforms/android/index.html, you need to:
install Android Studio
set the ANDROID_HOME environment variable to the location of your Android SDK installation.
It is also recommended that you add the Android SDK's tools, tools/bin, and platform-tools directories to your PATH.
Remove java.dll from C:\Windows\System32 (if any).
I'm not sure but It may work correctly.

"The system cannot find the file C:\ProgramData\Oracle\Java\javapath\java.exe"

I am on JDK 8u25 on Windows 8, and I am experiencing a problem with my Java installation. I can run javac perfectly fine, but running java produces this error message:
The system cannot find the file C:\ProgramData\Oracle\Java\javapath\java.exe
How do I fix this?
I think the problem has to do with my environment variables.
The important variables, as I have set them, are:
JAVA_HOME – C:\Program Files\Java\jdk1.8.0_25
CLASSPATH – .;%JAVA_HOME%\lib
PATH – <other paths omitted>;%JAVA_HOME%\bin
And their expansions, as viewed with set in cmd, are:
JAVA_HOME – C:\Program Files\Java\jdk1.8.0_25
CLASSPATH – .;C:\Program Files\Java\jdk1.8.0_25\lib;%CLASSPATH%;
PATH – C:\ProgramData\Oracle\Java\javapath;C:\Program Files\Java\jdk1.6.0_45\bin;<other paths omitted>;C:\Program Files\Java\jdk1.8.0_25\bin
The full output of set can be viewed here.
I've got a similar problem I'm currently working on solving. I can't say this will solve your error since there may be issues with other software (which is my case with Eclipse Luna).
Java is using the path given in your PATH variable, which is,
C:\ProgramData\Oracle\Java\javapath
Go to this folder (ProgramData is hidden, so unhide it if necessary) and right click on the java.exe symlink. You'll see in the Target field where it's looking for java.exe. It may be pointing to the java.exe file from your previous jdk1.6.0_45 install. Either paste in new shortcuts here or change the PATH setting as others have mentioned.
I've found that the Java installer doesn't change the javapath or update the system path, at least when going from 1.8 to 1.7. In my case changing the shortcuts only led to a problem with Eclipse on startup.
EDIT:-----
After playing with javapath folder shortcuts it turns out you need symlinks, at least for Eclipse. See, Windows how to create directory symlink.
You don't need the /d switch for a file symlink,
mklink java.exe "C:\Program Files\Java\jdk1.8.0_20\bin\java.exe"
mklink javaw.exe "C:\Program Files\Java\jdk1.8.0_20\bin\javaw.exe"
mklink javaws.exe "C:\Program Files\Java\jdk1.8.0_20\bin\javaws.exe"
This now works for me.
Why Oracle did such a poor way to point to java is beyond me. We solved this problem by creating a new link to the JDK
mklink /d C:\ProgramData\Oracle\Java\javapath "C:\Program Files\Java\jdk1.8.0_40\bin\"
The same would work for a JRE if that is all that is required.
This replaces the old symlinks in C:\ProgramData\Oracle\Java\javapath (if they existed previously)
I had in PATH:
C:\ProgramData\Oracle\Java\javapath;C:\Program Files\Java\jdk1.8.0_92\bin;<others omitted>
I removed:
C:\ProgramData\Oracle\Java\javapath;
and that fixed the issue for me. java -version now gives details about the Java version, etc.
This will solve all problems relating to Java and environment variables:
Make your way to Windows' Environment Variables dialog.
Under System variables, select the variable named Path. Click Edit...
Remove the entry that looks like:
C:\ProgramData\Oracle\Java\javapath
Add the path of your JDK/JRE's bin folder.
Don't forget to set JAVA_HOME.
I got same error while running JAVA command. To resolve this, I moved the java path as the first entry in the path, and it resolved the issue. Please have look at this screenshot for reference:
Updating the PATH Environment Variable
If you do not set the PATH variable, you need to specify the full path to the executable file every time you run it, such as:
C:\> "C:\Program Files\Java\jdk1.8.0\bin\javac" MyClass.java
It is useful to set the PATH variable permanently so it will persist after rebooting.
To set the PATH variable permanently, add the full path of the jdk1.8.0\bin directory to the PATH variable. Typically, this full path looks something like
C:\Program Files\Java\jdk1.8.0\bin. Set the PATH variable as follows on Microsoft Windows:
Click Start, then Control Panel, then System.
Click Advanced, then Environment Variables.
Add the location of the bin folder of the JDK installation to the PATH variable in System Variables.
The following is a typical value for the PATH variable:
C:\WINDOWS\system32;C:\WINDOWS;C:\Program Files\Java\jdk1.8.0\bin
Note:
The PATH environment variable is a series of directories separated by semicolons (;) and is not case-sensitive. Microsoft Windows looks for programs in the PATH directories in order, from left to right.
You should only have one bin directory for a JDK in the path at a time. Those following the first instance are ignored.
If you are not sure where to add the JDK path, append it.
The new path takes effect in each new command window you open after setting the PATH variable.
There are 2 versions of jdk in your PATH VARIABLE jdk1.6.0_45 and jdk1.8.0_25. Try removing the first one ie. jdk1.6.0_45 from the PATH
If you're on a corporate PC that's fairly restricted by group policy, this might work....
Assuming that
your Windows PATH includes C:\ProgramData\Oracle\Java\javapath
you have JDK installed to C:\Program Files\Java\jdk1.8.0_60\bin
Then create the following text file mklink.bat and put it on your desktop:
rem mklink.bat
mklink /d C:\ProgramData\Oracle\Java\javapath "C:\Program Files\Java\jdk1.8.0_60\bin"
pause
Now right-click it and choose "Run as Administrator". Provide admin credentials. The script should report success. Now you can compile or run Java.
This usually happens when you update the java,
the easiest way to solve this is to just uninstall the JDK & then reinstall it.
NOTE: This doesnt remove the path or classpath so no need to worry.
Uninstall all JDKs installed on your computer from the Java Control Panel
Search for C:\ProgramData\Oracle\Java and delete that directory and all files contained within. You can do this from the command line using rmdir /S C:\ProgramData\Oracle\Java
Then search for C:\ProgramData\Oracle and delete the oracle folder. You can do this using rmdir /S C:\ProgramData\Oracle
Now install JDK and set the path.
Run the program.You won't find the same problem anymore.
This problem exists when you upgrade from one version to another.because jdk is not automatically upgraded.
For the same you can change the environmental varibles.
In system variables look for the PATH and add the jdk bin location in the front of the string(not at the back).
Once you have done that check in CMD if "java" and "javac" works.
if it works, again go to system variables.
add "CLASSPATH" A the variable and set value " .
c:\Program Files\Java\jdk1.8.0_91\lib;"
There must be two or more PATH variables. Try merging all of them into one using semi-colon (;)
I got the same after installing java8 from a non-permissioned account.
To fix I simply reinstalled from admin user account.
This created the quoted directory with file links to java exes.
I had also similar problem where by I had to un-install JDK 1.8 and needed jdk 1.7.
What i did was removed the symbolic links from the javapath and then imported the shortcuts of java, javaw, javaws from the bin directory to the javapath folder. However, I found some permission issues in the enterprise laptop where by I did not have the privilege to modify/ update this directory. I had given appropriate permission from the administrator and there by resolved it.
c:\ProgramData\Java\javapath is used for symlinks. You can of course add the full path to your Java Path to %PATH%, but equally you can create a symlink to the path to the above location.
Open CMD as Administrator.
Type mklink java.exe (full path to your Java.exe)
eg
mklink java.exe "C:\Program Files\Java\jdk1.8.0_25\bin\java.exe"
Don't worry.
Just uninstall jdk as well as jdk updates
Before re installing jdk ,delete the oracle folder inside programData hidden folder in C:\
Then reinstall.
Set the following,
JAVA_HOME
CLASSPATH
PATH
JRE_HOME ( is optional)
Please remove "C:\ProgramData\Oracle\Java\javapath\java.exe" from the Path variable and add your jdk bin path. It will work.
In my case the I have removed the the above path and added my JDK path which is "C:\Program Files\Java\jdk1.8.0_221\bin"
For New version of Java JavaPath folder is located
64 bit OS
"C:\Program Files \Common Files\Oracle\Java\javapath\"
X86
"C:\Program Files(x86) \Common Files\Oracle\Java\javapath\"
This will solve all problems relating to Java and environment variables:
Make your way to Windows' Environment Variables dialog.
Under System variables, select the variable named Path. Click Edit...
Remove the entry that looks like:
C:\ProgramData\Oracle\Java\javapath
Add the path of your JDK/JRE's bin folder.
Don't forget to set JAVA_HOME.
This helped me.......:-)
For me it worked like this:
On your desktop go to Search->View advanced system settings->Environment Variables
Under System Variables look for path and click Edit.
Click on New and add the new Path and move it to the top.
To find your java path if you don't know where it is installed in cmd type where javac
<>\Common Files\Oracle\Java\javapath\ is now created as a junction, no symlinks anymore

Java command not found

I'm trying to get the java command to work on my computer. Whenever I try to use it, I get the error "'java' is not recognized as an internal or..."
What I've done
So far I've traced the issue through my system environment variables, which has a variable PATH that links to "C:\ProgramData\Oracle\Java\javapath" or javapath for short.
There are 3 files in the javapath folder, and if you click on them a popup says:
But when I examine the properties of the shortcut and click "open folder location" it sends me to the right place (?? what is this).
Just in case, I tried to recreate the shortcut; I called it java2 and it now works (opens a command box for a split second and closes) when I click on it. However, testing it in command prompt with the line "java2 -version" returns "'java2' is not recognized as an internal or external..."
Details
I just installed java 8, and I've verified that it was installed correctly
I'm running a 64 bit windows 7 OS
Have you checked your environment variables for Windows? When you installed the JDK/JRE the directory should have been added to the PATH variable. The directory added needs to contain the java.exe executable. It should be located in C:\Program Files\Java or something like that if you installed it using the default settings. Once you find that directory find the bin directory inside it.
In order to check that:
Right click My Computer and hit Properties
On the left side hit Advanced System Settings
Hit the Advanced tab and then Environment Variables down at the bottom
Find the PATH variable and then hit edit. Confirm that the location of the JDK /bin directory is there, if it is not then add it.
Also, if the JAVA_HOME variable isn't already set, this may be a good idea.

How can I set the PATH variable for javac so I can manually compile my .java works?

Here's the address on my drive:
C:\Program Files\Java\jdk1.6.0_18\bin
How would I go about setting the path variable so I can go in command window (windowskey+r "cmd") and be able to type things like:
javac TestApp.java
I'm using Windows 7 Professional.
That would be:
set "PATH=%PATH%;C:\Program Files\Java\jdk1.6.0_18\bin"
You can also append ;C:\Program Files\Java\jdk1.6.0_18\bin to the PATH in the user environment dialog. That would allow you to use javac and other java tools directly form any cmd shell without setting the path first. The user environment dialog used to be somewhere in the system properties in XP, I have no idea where it is in Windows 7.
Typing the SET PATH command into the command shell every time you fire it up could get old for you pretty fast. Three alternatives:
Run javac from a batch (.CMD) file. Then you can just put the SET PATH into that file before your javac execution. Or you could do without the SET PATH if you simply code the explicit path to javac.exe
Set your enhanced, improved PATH in the "environment variables" configuration of your system.
In the long run you'll want to automate your Java compiling with Ant. But that will require yet another extension to PATH first, which brings us back to (1) and (2).
Step 1: Set the PATH variable JAVA_HOME to the path of the JDK present on the system.
Step 2: in the Path variable add the path of the C:\Program Files\Java\jdk(version)\bin
This should solve the problem.
Happy coding!!
Type cmd in program start
Copy and Paste following on dos prompt
set PATH="%PATH%;C:\Program Files\Java\jdk1.6.0_18\bin"
Follow the steps given here
http://www.javaandme.com/
after setting variable, just navigate to your java file directory in your cmd and type
javac "xyx.java"
or if you don't navigate to the directory, then simply specify the full path of java file
javac "/xyz.java"
First thing I wann ans to this imp question: "Why we require PATH To be set?"
Answer : You need to set PATH to compile Java source code, create JAVA CLASS FILES and allow Operating System to load classes at runtime.
Now you will understand why after setting "javac" you can manually compile by just saying
"Class_name.java"
Modify the PATH of Windows Environmental Variable by appending the location till bin directory where all exe file(for eg. java,javac) are present.
Example : ;C:\Program Files\Java\jre7\bin.
only this will work:
path=%set path%;C:\Program Files\Java\jdk1.7.0_04\bin
You don't need to do any complex command-line stuff or edit any system code.
You simply have to open Computer, showing all of your disks and open properties.
From there, go to Advanced System Settings and click Environment Variables.
Scroll down in the lower list box and edit Path.
Do not erase anything already there. Put a ; after it and then type in your path. To test, open command prompt and do "javac", it should list around 20 programs.
You would be finished at that point.
By the way, the command to compile is javac -g not just javac.
Happy coding!
Trying this out on Windows 10, none of the command-line instructions worked.
Right clicking on "Computer" then open Properties etc. as the post by Galen Nare above already explains, leads you to a window where you need to click on "new" and then paste the path (as said: without deleting anything else). Afterwards you can check by typing java -version in the command-line window, which should display your current java version, if everything worked out right.

Categories