spalac24
spalac24

Reputation: 1116

Main class not found in .jar file despite the .jar contains the files

I have a .jar with a main class that I want to access. However, I have not been able to do so.

I first tried modifying the MANIFEST, but java always complained about

Error: Could not find or load main class

So I directly started using the -classpath flag like this:

java -classpath add2.jar add2.AddClass

However, it still gives me the same error. If I do jar tvf add2.jar it will give me the following output:

     0 Tue Jun 30 11:49:48 COT 2015 META-INF/
    95 Tue Jun 30 11:49:48 COT 2015 META-INF/MANIFEST.MF
     0 Fri Dec 05 09:09:22 COT 2014 add2/
169482 Fri Dec 05 09:09:18 COT 2014 add2/add2.ctf
  1786 Fri Dec 05 09:09:22 COT 2014 add2/Add2MCRFactory.class
  3848 Fri Dec 05 09:09:22 COT 2014 add2/AddClass.class
   318 Fri Dec 05 09:09:22 COT 2014 add2/AddClassRemote.class
 12288 Tue Jun 30 11:49:44 COT 2015 META-INF/.MANIFEST.MF.swp

So I suspect the add2.AddClass is in fact present. I have tried also AddClass without the package, with no luck.

Edit

I modified the MANIFEST by unzipping and zipping back together the .jar, using the jar command.

If I unzip the .jar. and execute javap add2/AddClass.class I will get:

Compiled from "AddClass.java"
public class add2.AddClass extends com.mathworks.toolbox.javabuilder.internal.MWComponentInstance<add2.AddClass> {
    public add2.AddClass() throws com.mathworks.toolbox.javabuilder.MWException;
    ...
    public static void main(java.lang.String[]);
    ...
    static {};
}

Upvotes: 1

Views: 1546

Answers (1)

thst
thst

Reputation: 4602

The reason is, that your AddClass inherits from com.mathworks... class.. But you did not add this class to the classpath. To generate a packed jar file, you need to use a special classloader like onejar (http://one-jar.sourceforge.net/). Otherwise just add the missing jars to the -cp classpath list.

This sample here will not throw a ClassNotFound Exception but a Main class cannot be found or loaded error.

  • compile class Test and Test2
  • remove Test2 from the classpath
  • run java -cp . Test

The Mainclass will not be loaded.

import bla.Test2;

public class Test extends Test2 {

    public static void main(String[] args) {
        String test = "TEST1";

        System.out.println(test);

    }
}

Where Test2.java is

package bla;

public class Test2 {
    public Test2() {
        String test = "TEST2";

        System.out.println(test);
    }
}

Upvotes: 2

Related Questions