I've been learning java on my own and I've come to a point where I need to pass arguments from the command prompt. I had previously been using Netbeans, which has become a bit of a crutch, but I want to learn how to program using notepad and the command prompt. I am running Windows 8.1. I downloaded the JDK 7 to my C:\ directory. I tried typing "java -version" and "javac -version" to check it out (as my book says to do) but it tells me "'java' is not recognized as an internal or external command, operable program or batch file." It says the same when I try javac. I do this from C:\ and from C:\Java> and get the same result. I uninstalled Netbeans, thinking maybe it was interfering somehow ... still not working. The book I'm using says it covers Java 7.
I don't understand why it doesn't work. Is there some compatibility issue with Java 7 and win 8.1? My java 7 is update 71 or 72. Did this update change something and now arguments are passed differently? Can anyone help me out? Thank you.
You need to download the JDK (if you don't already have it) and add the location to the end of your PATH. In a single command line session you can do this with PATH=%PATH%;<jdk location> where <jdk location> is the bin folder of the jdk.
Some installs of java manage environment variables and some do not. It appears yours has not. or you have not installed it correctly.
many applications understand JAVA_HOME, so you should set that to the root of the installation in your environment variables.
You will also need to add the bin folder to your path.
Generally, when using windows, you can install a windows exe version, which will manage all of this for you. Other installations just copy themselves to the c drive and expect java applications to know where to look.
Go to System environment variable. Select Variable PATH. Click on edit button. Append a ; after current value of Variable value. Copy jdk's bin folder path. In my PC it is "C:\Program Files\Java\jdk1.8.0_20\bin" without quotes. Then paste jdk's bin path. Copy jre's bin folder path. In my PC it is "C:\Program Files\Java\jdk1.8.0_20\jre\bin". Append a ; then paste jre's bin folder path. Click OK.
Download the JDK from this page. Run the installer.
Open the Command Prompt. Try it. It works. Yay!
If you use the full path to your java.exe, you dont need to set up any PATH and JAVA_HOME:
c:\> cd \work
c:\work> c:\java\bin\java.exe -cp classes\ your.Main
If your book does not use the normal JDK installers it really is supposed to explain that. (However it is normal behaviour for any executable).
(The above example asumes you compile into c:\work\classes\your\Main.class and your JDK is installed (installer does the unpack) in C:\java (typically you would use default locations like C:\Program Files\Java\jdk1.8.0\bin\java.exe).
I have a laptop with a Windows 7 OS 64-bit. I'm trying to set up the Java compiler so when I type in "javac" in the command prompt, it will compile the Java file I want. I've had it done on my laptop before but someone did it for me. They went to the Environment Variables under the Advanced System settings, then edited the "PATH" so that it would recognized the compiler. So I did the exact same thing, I copied the file location of javac from ProgramFiles, and put it in PATH. It still does not compile my Java files. It says:
"javac" is not recognized as an internal or external command.
And before you ask, I did search through other forums on this site to see if there was an answer that can help. There are similar situations to mine but the solutions did not help me.
As of now, this is the file path that I tried copying into PATH:
C:\Program Files\Java\jdk1.7.0_25\bin This leads to a file marked javac. So far I'm still unsuccessful with setting up the compiler.
It looks correct. Did you restart the Command prompt every time you changed the PATH variable ? Also there's supposed to be only one path variable, so you're supposed to edit the existing one, entries are separated by a semi-colon (;).
I want to execute my program without using an IDE.
I've created a jar file and an exectuable jar file. When
I double click the exe jar file, nothing happens, and when I try to use the command in cmd it gives me this:
Error: Unable to access jarfile <path>
I use the command: java -jar Calculator.jar
How I created the jar:
Right click on project folder (Calculator)
Select
Click on Java Folder and select "Exectuable Jar File", then select next
Launch Configuration: Main - Calculator
Create Export Destination
Hit "Finish" and profit! Well, not really.
I had encountered this issue when I had run my Jar file as
java -jar TestJar
instead of
java -jar TestJar.jar
Missing the extension .jar also causes this issue.
Fixed
I just placed it in a different folder and it worked.
[Possibly Windows only]
Beware of spaces in the path, even when your jar is in the current working directory. For example, for me this was failing:
java -jar myjar.jar
I was able to fix this by givng the full, quoted path to the jar:
java -jar "%~dp0\myjar.jar"
Credit goes to this answer for setting me on the right path....
I had this issue under CygWin in Windows. I have read elsewhere that Java does not understand the CygWin paths (/cygdrive/c/some/dir instead of C:\some\dir) - so I used a relative path instead: ../../some/dir/sbt-launch.jar.
I had the same issue when trying to launch the jar file. The path contained a space, so I had to place quotes around. Instead of:
java -jar C:\Path to File\myJar.jar
i had to write
java -jar "C:\Path to File\myJar.jar"
Just came across the same problem trying to make a bad USB...
I tried to run this command in admin cmd
java -jar c:\fw\ducky\duckencode.jar -I c:\fw\ducky\HelloWorld.txt -o c:\fw\ducky\inject.bin
But got this error:
Error: unable to access jarfile c:\fw\ducky\duckencode.jar
Solution
1st step
Right click the jarfile in question. Click properties.
Click the unblock tab in bottom right corner.
The file was blocked, because it was downloaded and not created on my PC.
2nd step
In the cmd I changed the directory to where the jar file is located.
cd C:\fw\ducky\
Then I typed dir and saw the file was named duckencode.jar.jar
So in cmd I changed the original command to reference the file with .jar.jar
java -jar c:\fw\ducky\duckencode.jar.jar -I c:\fw\ducky\HelloWorld.txt -o c:\fw\ducky\inject.bin
That command executed without error messages and the inject.bin I was trying to create was now located in the directory.
Hope this helps.
None of the provided answers worked for me on macOS 11 Big Sur. The problem turned out to be that programs require special permission to access the Desktop, Documents, and Downloads folders, and Java breaks both the exception for directly opened files and the permission request popup.
Fixes:
Move the .jar into a folder that isn’t (and isn’t under) Documents, Desktop, or Downloads.
Manually grant the permission. Go to System Preferences → Security and Privacy → Privacy → Files and Folders → java, and check the appropriate folders.
I had a similar problem and I even tried running my CMD with administrator rights, but it did not solve the problem.
The basic thing is to make sure to change the Directory in cmd to the current directory where your jar file is.
Do the following steps:
Copy jar file to Desktop.
Run CMD
Type command cd desktop
Then type java -jar filename.jar
This should work.
Edit: From JDK-11 onwards ( JEP 330: Launch Single-File Source-Code Programs )
Since Java 11, java command line tool has been able to run a single-file source-code directly. e.g.
java filename.java
If you are using OSX, downloaded files are tagged with a security flag that prevents unsigned applications from running.
to check this you can view extended attributes on the file
$ ls -l#
-rw-r--r--# 1 dave staff 17663235 13 Oct 11:08 server-0.28.2-java8.jar
com.apple.metadata:kMDItemWhereFroms 619
com.apple.quarantine 68
You can then clear the attributes with
xattr -c file.jar
It can also happen if you don't properly supply your list of parameters. Here's what I was doing:
java -jar test#gmail.com testing_subject file.txt test_send_emails.jar
Instead of the correct version:
java -jar test_send_emails.jar test#gmail.com testing_subject file.txt
This worked for me.
cd /path/to/the/jar/
java -jar ./Calculator.jar
For me it happens if you use native Polish chars in foldername that is in the PATH.
So maybe using untypical chars was the reason of the problem.
sometime it happens when you try to (run or create) a .jar file under /libs folder by right click it in android studio. you can select the dropdown in top of android stuio and change it to app. This will work
My particular issue was caused because I was working with directories that involved symbolic links (shortcuts). Consequently, trying java -jar ../../myJar.jar didn't work because I wasn't where I thought I was.
Disregarding relative file paths fixed it right up.
In my case the suggested file name to be used was jarFile*.jar in the command line. The file in the folder was jarFile-1.2.3.jar . So I renamed the file to jarFile. Then I used jarFile.jar instead of jarFile*.jar and then the problem got resolved
It can happen on a windows machine when you have spaces in the names of the folder. The solution would be to enter the path between " ".
For example:
java -jar c:\my folder\x.jar -->
java -jar "c:\my folder\x.jar"
To avoid any permission issues, try to run it as administrator. This worked for me on Win10.
I know this thread is years ago and issue was fixed too. But I hope this would helps someone else in future since I've encountered some similar issues while I tried to install Oracle WebLogic 12c and Oracle OFR in which its installer is in .jar format. For mine case, it was either didn't wrap the JDK directory in quotes or simply typo.
Run Command Prompt as administrator and execute the command in this format. Double check the sentence if there is typo.
"C:\Program Files\Java\jdk1.xxxxx\bin\java" -jar C:\Users\xxx\Downloads\xxx.jar
If it shows something like JRE 1.xxx is not a valid JDK Java Home, make sure the System variables for JAVA_HOME in Environment Variables is pointing to the correct JDK directory. JDK 1.8 or above is recommended (2018).
A useful thread here, you may refer it: Why its showing your JDK c:program files\java\jre7 is not a valid JDK while instaling weblogic server?
For me it happen because i run it with default java version (7) and not with compiled java version (8) used to create this jar.
So i used:
%Java8_64%\bin\java -jar myjar.jar
Instead of java 7 version:
java -jar myjar.jar
I had a similar problem where TextMate or something replaced the double quotes with the unicode double quotes.
Changing my SELENIUM_SERVER_JAR from the unicode double quotes to regular double quotes and that solved my problem.
this is because you are looking for the file in the wrong path
1. look for the path of the folder where you placed the file
2. change the directory cd in cmd use the right path
I use NetBeans and had the same issue. After I ran build and clean project my program was executable. The Java documentation says that the build/clean command is for rebuilding the project from scratch basically and removing any past compiles. I hope this helps. Also, I'd read the documentation. Oracle has NetBeans and Java learning trails. Very helpful. Good luck!
Maybe you have specified the wrong version of your jar.
I finally pasted my jar file into the same folder as my JDK so I didn't have to include the paths. I also had to open the command prompt as an admin.
Right click Command Prompt and "Run as administrator"
Navigate to the directory where you saved your jdk to
In the command prompt type: java.exe -jar <jar file name>.jar
Keep the file in same directory where you are extracting it. That worked for me.
This is permission issue, see if the directory is under your User.
That's why is working in another folder!
Rename the jar file and try
Explanation :
yes, I know there are many answers still I want to add one point here which I faced.
I built the jar and I moved it into the server where I deploy (This is the normal process)
here the file name which I moved already existed in the server, here the file will override obviously right. In this case, I faced this issue.
maybe at the time of overriding there can be a permission copy issue.
Hope this will help someone.
Have you tried to run it under administrator privoleges?
meaning, running the command in "Run As" and then select administrator with proper admin credentials
worked for me
I was trying this:
After giving the file read, write, execute priviledges:
chmod 777 java-repl.jar
alias jr="java -jar $HOME/Dev/java-repl/java-repl.jar"
Unable to access bla bla..., this was on Mac OS though
So I tried this:
alias jr="cd $HOME/Dev/java-repl/ && java -jar java-repl.jar"
This did not work "Unable to access jarfile"
"C:\Program Files\java\jdk-13+33-jre\bin\javaw.exe" -jar "C:\Program Files\Maxim Integrated Products\1-Wire Drivers x64\ OneWireViewer.jar"
This does work
"C:\Program Files\java\jdk-13+33-jre\bin\javaw.exe" -jar "C:\Program Files\Maxim Integrated Products\1-Wire Drivers x64\OneWireViewer.jar"
The difference is the single space in front of OneWireViewer.jar not withstanding that it is surrounded with quotes and even has other spaces.
javac is not internal or external command error is coming. I have set the path. then also it is giving the same error.
Are you sure you installed the JDK?
The JRE (aka "Java Runtime") does not contain javac, this is only part of the JDK ("Development Kit")
You should first examine the PATH by executing this command:
echo %PATH%
Among these folders should be at least one that looks similar to this:
C:\Program Files\Java\jdk1.6.0_20\bin
Maybe you left out the bin at the end, maybe there isn't such a folder name at all in your PATH? We cannot know. If you tried this and are still unsure, post the value of the PATH so we can give you real tips instead of just guessing.
If you're on windows, and running from a command prompt you need to reopen it to force any PATH changes to take effect
I downloaded this Java JDK and installed it.
But when I go to the command prompt to check the version, it says it's not recognized.
Is anyone else experiencing this issue with the latest Java?
I might not have installed the right version. I need the java that works with grails
C:\>java
'java' is not recognized as an internal or external command,
operable program or batch file.
C:\>java -version
'java' is not recognized as an internal or external command,
operable program or batch file.
C:\>
when i do a search on my computer for java, it does not find anything
Windows 2k8 R2 server-
For both java [-option] or %JAVA_HOME% to work in the command line you need the following:
In Control Panel->System and Security->System->Advanced system setting->Advanced->Environment Variables->System Variables
Edit the Path variable and add a ";" after the last value and add the the path to the Java bin directory:
e.g.- C:\Progra~2\Java\jre6\bin\
Add the JAVA_HOME Variable with the value set to the path for the java executable:
e.g.- C:\Progra~2\Java\jre6\bin\java.exe
Press simultaneously the "windows" and "pause" buttons on your keyboard, this will bring up the System Preferences dialog.
In the Advanced tab, find Environment Variables.
Then, in the User (upper) section, create or update the following two variables :
JAVA_HOME = where you put your JDK, eg. C:/Java/SDK
PATH = %JAVA_HOME%/bin
Close the dialogs.
Then, in a new command-line console, try "javac -version" and see if it's detected.
It's important that you use a new console, because environment variables are read only when the console is launched.
Java is typically installed (on Windows) as C:\Program Files\Java\jdk<version>
That installation directory has a subdirectory bin which you need to append to your PATH environment variable via the control panel. Then, the commands like java, javac etc. will be available on the command line.
BTW, the same is true for Grails.
Is the -version flag not recognized, or is the "java" command not recognized? One way to test this is just to type 'java' by itself and run it and see what happens.
If the command is not recognized, make sure that the JDK's install path is in your windows PATH. If not, you won't be able to use any of the java executables from the command prompt. Here's another link that may help out.
You need to manually add the path to javac.exe and java.exe to your operating system path. The Java installation program doesn't do that for you.
You most likely don't have java.exe in your system's PATH variable.
For Linux:
check $PATH and $JAVA_HOME. You can configure it in /etc/environment
From console you can check it like:
$ echo $PATH
For Windows:
My Computer -> Properties -> Advanced -> Environment Variables
Check there PATH.
From console you can check it like:
echo %PATH%
You should have a Java icon in Windows Control Panel. Locate the Java tab and click the View button. That will show you the path to the Java executables.
Last but not least, make sure you have restarted the computer so changes in the PATH variable can take effect.
You installed the JDK. Isn't java.exe part of the JRE? Do you have that installed?
Maybe your system variables in the environment variables are not set properly. Follow the steps in the link below. Finally, make sure the path component in system variables has only one JDK path. Delete other JDK paths that you won't use.
https://docs.oracle.com/javase/tutorial/essential/environment/paths.html
You might have installed a previous or older version of Java so you can just uninstall it and directly download JDK from Java JDK
I was also facing the same issue but when I downloaded and installed the latest version the issue was resolved.
Also uninstall any other Java versions which may be present in the program files.
To get to know other versions which may be present in your computer use Windows File Explorer to go to:
Files > Windows C > Program Files > Java
All the JDK and JRE you have will be listed there. Uninstall any other Java versions other than what you installed. And then make sure you have set the path variable of the JDK.
For me, it was incorrect line in the PATH
(1) Check PATH: Type "Edit System Environment Variables" on the search -> System Properties -> Advance -> Environment Variables -> System variables -> Path
(2) On the list, mine was C:\Program Files (x86)\Common Files\Oracle\Java\javapath so I just add \ at the end (the exe files in the javapath folder)
OS: Windows 10 64 bit
IF you set the PATH and it's not showing up in cmd when you run %PATH%, try restarting your computer.