[英]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. 我从eclipse中导出了一个jar文件,在jar文件中有Java类文件的各种包和子包。
I'd like to run one of these nested class files on the command line in Windows. 我想在Windows的命令行中运行这些嵌套类文件之一。
The class has a main and I am using the following to try to run it, 该类有一个main,我正在使用以下代码尝试运行它,
java -classpath .;./example.jar example
Note that example is the name of the class as well as the jar. 请注意,示例是类以及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. 并且如果发帖人或以后会读到此书的人对Unix不熟悉,以$开头的行是我在shell中键入的命令,其他所有内容都从这些命令输出。 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. Eclipse将为您处理前三个问题,然后最后一个
java -classpath a.jar xyzA
是执行xyzA类中的main方法的命令。
Just running java -cp example.jar the.whole.path.example
should do the trick. 仅运行
java -cp example.jar the.whole.path.example
应该可以解决问题。 If not, then something with your JAR file is wrong. 如果不是,则JAR文件中的内容有误。 The class name must be fully qualified (with package name) and the specified class must have a correct main method:
类名必须是完全限定的(带有包名),并且指定的类必须具有正确的main方法:
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: 如果罐子中包含很多条目,则可能要grep /查找相关的类:
jar tf example.jar | find /i "Example"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.