0

Java (and stackoverflow) newbie here, first post ever, so please be patient. Using windows command line interface I can't generate an executable .jar file, starting from a .class file. Seems like it should work, but it doesn't. Why, what am I missing?

I'm in the Windows cmd "black box", in a folder with Test.class, an app which simply displays "hello world" to the console. As per online videos, I try:

c:\junk\java -jar -cfv myJar.jar Test.class Error: Unable to access jarfile myJar.jar

I thought the -c switch meant create, so I don't understand the "unable to access"

btw, no problem compiling the .java to the .class using javac.
Windows 11, Java version "20.0.1" 2023-04-18

Glenn
  • 1
  • 1
  • 2
    `java -jar ...` is used to RUN a `jar`, not create one. To create a `jar` file, we can use the `jar` command. See [this post (and its answers)](https://stackoverflow.com/questions/9941296/how-do-i-make-a-jar-from-a-java-file) for detials. – Turing85 Aug 18 '23 at 20:50
  • To put it another way, remove the first five characters from your command. It should be just `jar -cfv myJar.jar Test.class` – VGR Aug 18 '23 at 20:54

2 Answers2

0

The Java compiler, javac, doesn't create jar files by itself. The usual method is to use the jar utility to build your jar file. Of course not too many people do it that way because the structure of the jar needs to be correct.

Most use a build tool such as ant (kinda old-school these days), Maven, or Gradle. Another option is one of the IDEs that have the tooling to build jars. I'm most familiar with Eclipse where you can Export a project to a jar file.

jwh20
  • 646
  • 1
  • 5
  • 15
0

Never mind, I figured out what I was doing wrong. "java -jar" does not do it, from command line; must be "jar" straight up. For some reason java.exe and jar.exe are not in the same folder. Evidently JDK install adds to the PATH environment variable to point to java.exe (and javac.exe). After adding the folder where jar.exe is to the PATH env var, then it worked like it should.

Thanks for your answer, true that this low-level way is probably not best, I will try other ways. But I like to understand bottom up.

Glenn
  • 1
  • 1
  • _java.exe and jar.exe are not in the same folder_ Of-course they are (in the same folder). – Abra Aug 20 '23 at 08:57