-1

I'm trying to create a jar file and run it using java -cp main.jar com.test.Foo.Main but I keep getting:

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

This is my file structure. So I'm thinking the line in my Main.java should be package com.test.Foo correct?

enter image description here

I'm compiling my Main.java with javac Main.java which outputs a Main.class file. Afterward, I create a jar file using jar cfm main.jar META-INF/MANIFEST.MF Main.class and finally while I'm in the same directory as the jar file <root>/src/com/test/Foo/ I run java -cp main.jar com.test.Foo.Main and that's when I run into the above error. Any idea how I can run this file like this (and yes I need it to run with this command specifically)?

Main.java

package com.test.Foo;

public class Main {
  public static void main (String args[]) {
    System.out.println("I am com.test.Foo.Main");
  }
}

META-INF/MANIFEST.MF

Manifest-Version: 1.0
Main-Class: com.test.Foo.Main

I tried using some of the options given in this popular SO question and nothing helped.

Brandon Benefield
  • 1,504
  • 1
  • 21
  • 36
  • "Main" is probably a bad name for a class ... but it sounds like your problem is that you forgot "Foo": CHANGE MANIFEST.MF: `Main-Class: com.test.Foo.Main`. I'd also suggest: `java -jar main.jar`. – paulsm4 Dec 04 '18 at 05:17
  • Sorry I had just added that into the error. I still get the same problem even if I include `Foo` -- `java -cp main.jar com.test.Foo` – Brandon Benefield Dec 04 '18 at 05:19
  • Well I'm not sure what a `pom` file is so I'm going to say no. Should this be my next step? – Brandon Benefield Dec 04 '18 at 05:29
  • how do you create your jar file ? what's your IDE ? – Mehdi Dec 04 '18 at 05:49
  • @Mehdi so I create the jar file using `jar cfm main.jar META-INF/MANIFEST.MF Main.class` and for this test project I'm just using VSCode. Typically, I would use IntelliJ. – Brandon Benefield Dec 04 '18 at 05:52
  • @BrandonBenefield ok wait I'm writing your answer – Mehdi Dec 04 '18 at 05:52

3 Answers3

1

The picture you're showing in your question is your project structure not your jar structure.

When you create a jar file, the structure for that jar file might be different with your source code folder structure.

Every IDE (such as eclipse, netbeans, IntelliJ) has a mechanism for creating JAR files. In your case when you open the created jar file (using zip apps like winrar) you should see something like this :

com
  |
  test
     |
     Foo
       |
       Main
META-INF
       |
       MANIFEST.MF

This should be the ordering of your files and folders, otherwise Java can not find your main class from MANIFEST.MF

Now to solve this problem:

  1. Open your jar file using a zip application like winrar
  2. check the folder structure residing inside your jar file as I draw
  3. Fix it right away within the winrar or try to correct your project structure to produce the structure I mentioned.
Mehdi
  • 3,795
  • 3
  • 36
  • 65
0

The class is called com.test.Foo.Main you need to specify the full name in the command:

java -cp main.jar com.test.Foo.Main

or you can use the simpler

java -jar main.jar
Henry
  • 42,982
  • 7
  • 68
  • 84
  • So running the `java -jar main.jar` returns `no main manifest attribute, in main.jar`. Ideas on why this is happening if I'm including the manifest file when jarring everything? – Brandon Benefield Dec 04 '18 at 05:34
0

Check your META-INF/MANIFEST.MF for the attribute of Manifest-Version: 1.0

This attribute must be there.


Edit:

You need to move to the source root src/ and issue below command to create a valid jar.

javac com/test/Foo/*.java

and, create the jar using,

jar cmf com/test/Foo/MANIFEST.MF main.jar com/test/Foo/*.class

The thing is, package structure should match with the folder structure apparently.

isuru89
  • 742
  • 11
  • 19