• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JAR command not working

 
Greenhorn
Posts: 7
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Java Gurus,

I've started preparing for my ocp exams for java se 17 for the last couple of weeks. I've completed most of the practical code examples given in the ocp study guide by Selikoff and Boyarsky but faced issues in the modules chapter. I have downloaded the jdk for java 17 from the oracle website. The java and javac commands work perfectly fine but I face issues when it comes to creating jars with the jar command from the cmd prompt.

I tried running the jar command by itself and here's the error message I got:






when I try java -jar I get this message:




I've tried reinstalling the entire jdk and tried updating my path variables but can't seem to resolve this issue. I've even tried the methods under this post on stack overflow   -->  https://stackoverflow.com/questions/29180639/java-jar-is-not-recognized-as-an-internal-or-external-command  but couldn't seem to fix it. Please help me resolve this issue.

I'm using a lenovo laptop with an AMD Ryzen 3 5300U with Radeon graphics (2.6GHz, x64 arch) processor with a windows 11 installation with all the latest updates.
 
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

'jar' is not recognized as an internal or external command,
operable program or batch file.


The path to the bin folder that contains the jar command needs to be in the PATH environment variable to allow the OS to find the command.
Check that folder to make sure there is a jar.exe file there.
If javac works, where is the javac.exe file?  Is there a jar.exe file in the same folder?

Alternately you can specify the full path to the command on the command prompt window.  For example:

D:\Java\jdk-17\bin\jar.exe   ...

 
Saloon Keeper
Posts: 27861
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch, Lakshmi!

One of the great things about Java is the simplicity of its applications, both the actual JDK and systems like Apache Tomcat. At their heart, each of those apps is self-contained and distributable as a simple ZIP file. A JAR file, by the way, is just a ZIP with a few special members added and a ".jar" extension, which is also true of other Java archives such as the WAR files deployed into webapps.

There is a Windows-based installer and it does add some stuff to the registry, as I recall, but it's inconsequential. The active part of the JDK is downloaded and unzipped and that's it. From there on out it's plain vanilla DOS. No magic, nothing OS-specific.

Common practice is to define an environment variable named JAVA_HOME that gives the filename path of the unzipped JDK directory. Java was designed to allow you to have multiple JDKs installed and running apps at the same time, so it's convenient to have this environment variable set, but not essential. JAVA_HOME isn't actually part of the Java standard, but it is a popular convention.

Your environment typically also defines a PATH that Windows will use to locate executable programs to run by defining the directory paths to look for scripts (batch files) and .EXE's. So by defining JAVA_HOME and adding %JAVA_HOME%\bin to that PATH, all of the JDK applications, including "java", "javac" and "jar" will be automatically found when issued as commands in a command shell.
 
Saloon Keeper
Posts: 10781
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:Your environment typically also defines a PATH that Windows will use to locate executable programs to run by defining the directory paths to look for scripts (batch files) and .EXE's. So by defining JAVA_HOME and adding %JAVA_HOME%\bin to that PATH, all of the JDK applications, including "java", "javac" and "jar" will be automatically found when issued as commands in a command shell.


Add the JAVA_HOME to the PATH at the beginning of the path.
 
Tim Holloway
Saloon Keeper
Posts: 27861
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:

Tim Holloway wrote:Your environment typically also defines a PATH that Windows will use to locate executable programs to run by defining the directory paths to look for scripts (batch files) and .EXE's. So by defining JAVA_HOME and adding %JAVA_HOME%\bin to that PATH, all of the JDK applications, including "java", "javac" and "jar" will be automatically found when issued as commands in a command shell.


Add the %JAVA_HOME%\BIN to the PATH at the beginning of the path.


I didn't actually say append, by the way. Windows PATH and environment is rather weird, so I left that as an exercise to the reader rather than to my untrustworthy memory.
 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The OP said:

javac commands work perfectly fine


That would imply the PATH was set to the bin with the javac command.  Why isn't the jar command also found there?
 
Carey Brown
Saloon Keeper
Posts: 10781
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oracle and Microsoft huddled together to come up with a way to install Java under windows. It doesn't quite follow normal conventions so I can't vouch for what's there except what's needed to support the run-time environment. Oracle always puts theirs first on the PATH which is why, if you install a different version, you have to put your first.
 
Lakshmi Narasimhan Srinivasan
Greenhorn
Posts: 7
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Update: I just graduated out of grade 12 with a majors in biology, so I'm kind of new to creating environment variables and couldn't understand what Carey Brown orTim Holloway said. Could you guys be more specific.
 
Carey Brown
Saloon Keeper
Posts: 10781
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. Locate "jar.exe". Using Windows File Explorer look in C:\Program Files\Java\jdk-17\bin. If you don't find it there then do you have another jdk directory listed?

2. On your task bar is a magnifying glass, click on that and enter "environment". It should give you the option "Edit System Environment Variables". Click on that.

3. Near-ish the bottom on the right is a button "Environment  Variables". Click on that.

4. Look to  see if the bottom section already has a variable "JAVA_HOME" (all caps). If not, click on the "New" for the bottom section and enter name = "JAVA_HOME" and value = "C:\Program Files\Java\jdk-17" (no bin), then select "OK".

5. Scroll bottom section down to the PATH (or "Path") variable.

6. Select that and click the Edit button.

7. The very first line should be
%JAVA_HOME%\bin

Use "new", "edit", "move up", and "move down" as needed.

8. Click "OK". Click "OK". Click "OK".

9. Bring up a new command window and type in "jar". What do you get?
 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have questions about how you could get the javac command to work but not the jar command.  They should be in the same folder and that folder should be on the PATH.  You said that javac worked. Can you look in the bin folder that is on the PATH and see if both javac.exe and jar.exe are in the folder?
 
Tim Holloway
Saloon Keeper
Posts: 27861
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:Oracle and Microsoft huddled together to come up with a way to install Java under windows. It doesn't quite follow normal conventions so I can't vouch for what's there except what's needed to support the run-time environment. Oracle always puts theirs first on the PATH which is why, if you install a different version, you have to put your first.



Unless it's changed a lot, I'd take that with a grain of salt. Microsoft spent years sulking over the fact that Sun had prevented them from hijacking Java (J++).  Also, as I said, the JDK is designed to be all in one place, so there's not a need for fancy installation processes. The main way that Windows differs from other OS's is in the fact that it is descended from a single-user single-tasking system, and isn't as accomodating to having multiple versions of anything running (especially Internet Explorer). A lack of versioning on DLLs and the fact that windows locks files by name rather than instance also takes its toll. The file locking is directly to blame for years of pain in having to reboot when even the most minor application upgrades were installed, as only by rebooting and running in single-tasking early-stage boot could you safely free all the locks and swap in the new files.

But in the end, Java is still fairly unscarred even in Windows.

The reason why you'd put the JDK bin directory first in the path is simple. It's actually the same reason you'd do that for ANY application, including Ant, Maven, and for that matter, non-Java applications. The PATH is searched by looking at each directory specified in the PATH in turn, starting with the beginning of the list and moving down until a program/script is found. Or not. You can generally assume that whatever you added latest is the most important to what you are doing at the moment, so by putting the JDK bin directory first, you ensure that when you say "jar", the command shell will run your JDK's jar program and not some other unrelated program that just also happens to be named jar and lives in one of the later directories in the path. Especially (but not limited to) some other JDK that's not the version you want to be working with.

I  try to be careful when talking about PATH in Windows, since A) unlike other OS's, which only work with PATH as an environment variable, Windows has a history of an actual PATH command. B) Windows also has 2 major environments, each of which can define environment variables. One is the system environment, one is the application (user) environment. I don't work with Windows anymore so I've fuzzed out on a lot of the rules.

Also, some information for Lakshmi: The Windows command shell has an associated resource, which is its environment. The environment holds vital information for running that shell instance (whether it's traditional COMMAND.COM or PowerShell). Environment variables are an important feature of the environment and they are simply name/value pairs where the values are strings. The command shell has certain variables that it uses to help it do what it needs to do. The PATH, for example, is used to find executables. The PROMPT can be set to alter what the shell prints out each time it is ready for you to type in a new command. You can for, example, change "C:\" to "Feed me, Seymour!" if you like. And you can also define and use your own made-up environment variables (which is what JAVA_HOME is). They can be referenced anyway you like. and Java apps can read them using standard Java API methods to pass information into apps as an alternative to command-line parameters.

The only caution is that if you change environment variable values, you know what you're doing. Setting a bad PATH can lead to painful results, for example!
 
Lakshmi Narasimhan Srinivasan
Greenhorn
Posts: 7
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Update: Thanks Carey for your detailed explanation. The jar command works now. Now I can continue preparing for my ocp exam prep.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic