-1

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:


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

when I try java -jar I get this message:


D:\>java -jar
Error: -jar requires jar file specification
Usage: java [options] <mainclass> [args...]
           (to execute a class)
...

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 --> text but couldn't seem to fix it. Pls 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.

  • As the error message said, you need a file "xxx.jar". I guess the real question is just what are you trying to do? What do you think the jar command should do? – cliff2310 Sep 02 '23 at 15:23
  • 2
    *'jar' is not recognized as an internal or external command,* would be caused by the bin directory of your JDK installation not being in `PATH`. Having said that, I don't know how *The java and javac commands work perfectly fine* since `javac` is in exactly the same directory as `jar` – g00se Sep 02 '23 at 15:30
  • This isn’t about Java, really. All command-line invocations work the same way: they have to be in a directory listed in the Path variable or in the current directory. (On non-Window systems, a command needs to be in a directory listed in the PATH variable.) – VGR Sep 02 '23 at 19:09

2 Answers2

0
Create JAR file:

compile the ".java"

   javac MyClass.java

Create "jar":

   jar cfe newJar.jar MyClass MyClass.class.

where: 

"newJar.jar" is the name of the jar to be created.

"MyClass" is the name of the main class.

"MyClass.class" is the name of the file containing the main class.
Marce Puente
  • 101
  • 1
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Diego Borba Sep 02 '23 at 20:48
-1

Format:java [-options] -jar jarfile [args...] you need specify a file ,like /path/xxx/demo.jar;

java -jar math-game.jar
qisi
  • 9
  • 1
  • 1
    Unrelated to the problem, which is *I face issues when it comes to creating jars with the jar command from the cmd prompt.* – g00se Sep 02 '23 at 15:27
  • I'm sorry I read it wrong,maybe you try execute jar command on jdk path(jar.exe and java.exe in the same path),Let's see if we can rule out environmental variables. – qisi Sep 02 '23 at 15:46
  • I don't think anyone knows what the question is. – cliff2310 Sep 02 '23 at 23:02