Home > Software engineering >  class file in JAR seems to be ignored; class file being picked up from directory on classpath instea
class file in JAR seems to be ignored; class file being picked up from directory on classpath instea

Time:06-11

I am trying to get a simple JAR file executing on the CLI (without a manifest). The folder structure is as follows:

mods\un.jar
out\test\Main.class
src\test\Main.java

Here is Main.java:

package test;

public class Main{
  public static void main(String []args){
    System.out.println("Unnamed module...");

  }
}

Here are the commands from the root folder:

javac -d out src\Main.java
java -cp out test.Main      - this works
jar -cvf mods\un.jar out\test\Main.class
java -cp mods\un.jar;out test.Main   - works (when 'out' is in classpath)
java -cp mods\un.jar test.Main - ERROR

The last line generates the errors:

Error: Could not find or load main class test.Main
Caused by: java.lang.ClassNotFoundException: test.Main

I have looked at the JAR zip and it looks ok. The MANIFEST.MF is the default one but that is okay as I am explicitly telling Java what class to start with (the class with main()). The contents are:

META-INF\MANIFEST.MF
out\test\Main.class

The manifest itself is:

Manifest-Version: 1.0
Created-By: 11 (Oracle Corporation)

So it looks like the class file in the JAR is not being picked up at all. If I omit the out folder when I run java or if I rename the .class file in the out folder), I get the class not found error. I would like to get it working without a manifest.

Any ideas?

Thanks, Seán.

CodePudding user response:

You need to change to the 'out' directory to get your package root correct in the jar:

jar -cvf mods\un.jar -C out test\Main.class
  • Related