0

Basically I was learning the compilation process of an executable jar file. So, for that purpose, inside a directory src\com\mftest, I have a file named Test.java, with the following contents.

package com.mftest;

public class Test
{    
    public static void main(String args[]) 
    {    
        System.out.println("Hello from Test!");
    }
}

Now, from the main folder (one containing the src folder), where I also have the folder bin, I type javac -d bin src\com\mftest\Test.java. This creates the file bin\com\mftest\Test.class.

Then, from within the folder bin\com\mftest, I create a file Manifest.txt with the following contents. Note that here there are two lines, with the 2nd line being blank.

Main-Class: com.mftest.Test

From within the bin\com\mftest folder, I now type jar cmvf Manifest.txt Test.jar *.class to create the file bin\com\mftest\Test.jar. I expected that file to be executable, but when I try to run it by typing java -jar Test.jar, I get the following error.

Error: Could not find or load main class com.mftest.Test
Caused by: java.lang.ClassNotFoundException: com.mftest.Test

Any idea, what mistake am I doing here?

AlexScalar
  • 1,729
  • 3
  • 10
  • 17
  • I have read all those answers, and that is how I came to know that I have to add this manifest file. I did that, but still getting the error. – AlexScalar Sep 20 '18 at 12:13
  • Sorry, but that doesn't work out. The very first answer there turns in **length** about how source paths and class paths must come together. And that com/mftest is part of the class name, and thus that the hierarchies must match up. – GhostCat Sep 20 '18 at 12:30
  • @GhostCat. Don't worry, I got it working now. – AlexScalar Sep 20 '18 at 12:32

1 Answers1

2

From within the bin\com\mftest folder, I now type jar cmvf Manifest.txt Test.jar *.class to create the file bin\com\mftest\Test.jar

I think I see your problem.

If you run jar tvf Test.jar I expect that you will see that path for the Test.class file within the JAR file is "/Test.class". It should be "/com/mftest/Test.class".

Your mistake was to change directory to bin\com\mftest. You should have created the JAR file from within the bin directory to get the pathnames in the JAR correct.

Note that the manifest file you include in the JAR could be anywhere. You just need to provide the correct (file system) path for it in the jar command.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216