简体   繁体   中英

Run a class in a sub package in a jar file on Windows command line

I exported a jar file from eclipse and in the jar file are various packages and sub packages with java class files.

I'd like to run one of these nested class files on the command line in Windows.

The class has a main and I am using the following to try to run it,

java -classpath .;./example.jar example

Note that example is the name of the class as well as the jar.

I've also tried to spell out the full path of the class

java -classpath .;./example.jar the.whole.path.example

How can I run the example class?

EDIT:

OK this is kind of stupid, the full path was incorrect. I checked this over numerous times yet it was only when I came back to it that I noticed the error.

Without seeing any of your code or any output (hint, hint) it's hard to say what's happening, but this works for me:

$ cat x/y/z/A.java
package x.y.z;

public class A
{
    public static void main(String args[]) throws Exception
    {
        System.out.println("here in A");
    }
}
$ javac x/y/z/A.java
$ jar cvf a.jar x/y/z/A.class
added manifest
adding: x/y/z/A.class(in = 459) (out= 311)(deflated 32%)
$ java -classpath a.jar x.y.z.A
here in A

And in case the poster or someone reading this in the future isn't familiar with Unix, the lines starting with $ are commands I type into the shell and everything else is output from those commands. Eclipse will take care of the first three for you, then the final java -classpath a.jar xyzA is the command to execute the main method in the xyzA class.

Just running java -cp example.jar the.whole.path.example should do the trick. If not, then something with your JAR file is wrong. The class name must be fully qualified (with package name) and the specified class must have a correct main method:

public static void main(String[] args) {

}

Check if your assumptions are correct. Show the structure of your jar and compare it to what you try to run:

jar tf example.jar

If the jar contains a lot of entries you might want to grep / find the relevant class:

jar tf  example.jar | find /i "Example"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM