开发者

Understanding JARs and Packages in Java

I'm trying to understand how jars and packages work in Java. So to do this, I created a simple test JAR and am trying to use a class contained in that jar. Simple enough, but it is giving me errors like "class not found". Here's the setup:

1) I have a file called MyHelloWorld.java, which will be packaged in a JAR:

package com.mytest;

public class MyHelloWorld {
    public String getHello() {
        return "Hello";
    }
    public static void main(String[] args) {
        System.out.println("Hello world!");
    }
}

2) I have another file called 'HelloHello.java' which uses the function getHello() in com.mytest.MyHelloWorld

import com.mytest.*;

public class HelloHello {
    public static void main (String[] args) {
        MyHelloWorld hello = new MyHelloWorld();
        System.out.println(hello.getHello());
    }
}

3) To package the MyHelloWorld class inside a JAR, I created the folders com/mytest in the current directory, and moved MyHelloWorld.java to that folder

4) I compiled MyHelloWorld.java in that folder using javac MyHelloWorld.java

5) I ran jar -cf myhello.jar ./com/mytest/*.class from the root folder to create the JAR file (as described in http://www.javacoffeebreak.com/faq/faq0028.html)

6) I copied HelloHello.java and myhello.jar to a new folder with nothing else in it, to test this setup

7) javac -cp ./*.jar HelloHello.java [succeeds]

8) java -cp ./*.jar HelloHello [FAILS] (I also tried just `java HelloWorld', which failed too, with a different error message)

This last statement fails with the message:

$java -cp ./*.jar HelloHello
Exception in thread "main" java.lang.NoClassDefFoundError: HelloHello
Caused by: java.lang.ClassNotFound开发者_运维技巧Exception: HelloHello
    at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:315)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:330)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:250)
    at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:398)

Any idea why it's failing? Any insights you can provide on why it works this way, and how package names are defined inside a JAR etc. would also be appreciated!


I believe it is looking in the jar for your HelloHello class. You probably need the current folder on the classpath too.

java -cp .:myhello.jar HelloHello


You should use:

java -cp .:./* HelloHello

java and javac treat -cp argument a bit differently. With java the * in cp will automatically load all the jars it finds in the given location.

Also, the colon : is the separator between different classpath elements.


Make sure if HelloHello.class is in appropriate directories structure (com/mytest) than change your 8th step:

8) java com.mytest.HelloHello //or java -cp .;*.jar com.mytest.HelloHello


well, java HelloHello works too

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜