开发者

Java: simple JAR project, when run, cannot find an imported class in a second simple JAR project even though second JAR passed via -classpath

I have written two simple Java classes (one of them containing "main()", and the other called by "main()").

Class #1 (containing "main()"):

package daniel347x.outerjar;
import daniel347x.innerjar.Funky;
public class App 
{
    public static void main( String[] args )
    {
        Funky.foo();
    }
}

Class #2 (called by "main()"):

package daniel347x.innerjar;
public class Funky 
{
    public static void foo()
    {
        System.out.println( "Funky!" );
    }
}

The above classes appear in different project root folders, and use Maven as the build system (each project has its own POM). The pom.xml file for the main project includes the proper entry to add daniel347x.outerjar.App as the main class, and it properly includes the dependency on daniel347x.innerjar. Both projects build successfully into JAR files.

I use NetBeans to wrap these as Maven projects (in fact, I used NetBeans to create both projects). When I run the main project from within NetBeans, it runs successfully and I see Funky! as the output.

However, when I attempt to run the main class straight from the Windows command line (cmd.exe), passing the JAR file containing Funky on the command line's classpath, as such:

java -classpath "P:\_Dan\work\JavaProjects\JarFuckup\innerjar\target\innerjar-1.0-SNAPSHOT.jar" -jar "P:\_Dan\work\JavaProjects\JarFuckup\outerjar\target\outerjar-1.0-SNAPSHOT.jar"

.开发者_如何学JAVA.. I receive the dreaded NoClassDefFoundError:

Exception in thread "main" java.lang.NoClassDefFoundError: daniel347x/innerjar/Funky
    at daniel347x.outerjar.App.main(App.java:7)

I have carefully confirmed that, inside the innerjar JAR file noted above containing Funky, that the path structure is daniel347x\innerjar and that inside the innerjar folder is the Funky.class file - and this file looks correct within a HEX editor where I can see the ASCII strings representing the name of the class.

The fact that the class can't be found defies my understanding of Java, which I thought allows you to pass JAR files as a -classpath parameter and it will be able to find classes inside those JAR files.

This very basic point has me flummoxed - an answer that explains what I am doing wrong would be greatly appreciated.


The classpath is ignored when using the -jar option. A way to run your app would be java -classpath "P:\_Dan\work\JavaProjects\JarFuckup\innerjar\target\innerjar-1.0-SNAPSHOT.jar";"P:\_Dan\work\JavaProjects\JarFuckup\outerjar\target\outerjar-1.0-SNAPSHOT.jar" daniel347x.outerjar.App


Perhaps a better approach would be to add a manifest file to the Jar that specifies the class path of the dependent Jars by relative paths. Then..

java -jar "P:\_Dan\...\outerjar-1.0-SNAPSHOT.jar"

..should do it.

Double clicking the main Jar will also launch it. That is mostly useful for GUIs.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜