[英]Java Command Line Trouble with Reading a Class from a Jar Archive
I am trying to run a java based tool using a command line syntax as the following: java -cp archive.jar archiveFolder.theMainClassName.Although the class I am searching for, a main class, "theMainClassName" is in the archive.jar and in the archiveFolder given at input, I keep getting the error that my class is not seen. 我正在尝试使用命令行语法运行基于java的工具,如下所示:java -cp archive.jar archiveFolder.theMainClassName。虽然我正在搜索的类是一个主类,“theMainClassName”在archive.jar中,在输入中给出的archiveFolder中,我不断收到我的类没有看到的错误。 Does anybody have any ideas concerning this problem?
有没有人对这个问题有任何想法? Thank you in advance
先感谢您
Here's a concrete example of what does work, so you can compare your own situation. 这里是做什么工作的一个具体的例子,这样你可以比较你自己的情况。
Take this code and put it anywhere, in a file called MainClass.java
. 将此代码放在任何位置,放在名为
MainClass.java
的文件中。 (I've assumed a directory called src
later. Normally you'd arrange the source to match the package, of course.) (我以后假设了一个名为
src
的目录。当然,通常你会安排源匹配包。)
package archiveFolder;
public class MainClass
{
public static void main(String[] args)
{
System.out.println("I'm MainClass");
}
}
Then run each of these commands: 然后运行以下每个命令:
# Compile the source
javac -d . src/MainClass.java
# Build the jar file
jar cf archive.jar archiveFolder
# Remove the unpackaged binary, to prove it's not being used
rm -rf archiveFolder # Or rmdir /s /q archiveFolder on Windows
# Execute the class
java -cp archive.jar achiveFolder.MainClass
The result: 结果:
I'm MainClass
How are you building your jar file? 你是如何构建jar文件的? Is the code in the appropriate package?
代码是否在相应的包中?
Does theMainClassName class have the following package line at the top: theMainClassName类的顶部是否包含以下包行:
package archiveFolder
You need the class file to be in the same directory structure as the declared package. 您需要类文件与声明的包在同一目录结构中。 So if you had something like:
所以,如果你有类似的东西:
org/jc/tests/TestClass.class
its source file would have to look like this: 它的源文件必须如下所示:
package org.jc.tests;
public class TestClass {
public static void main(String[] args) {
System.out.printf("This is a test class!\n");
}
}
Then you could use the following to create the jar file and run it from the command line (assuming the current directory is at the top level, just above org): 然后,您可以使用以下命令创建jar文件并从命令行运行它(假设当前目录位于顶层,就在org之上):
$ jar -cf testJar.jar org/jc/tests/*.class
$ java -cp testJar.jar org.jc.tests.TestClass
Perhaps with java -jar archive.jar
? 也许用
java -jar archive.jar
?
Of course, it supposes the manifest points to the right class... 当然,它假定了正确阶级的明显点......
You should give the exact message you got, it might shed more light. 你应该给出你得到的确切信息,它可能会带来更多的光。
EDIT: See Working with Manifest Files: The Basics for information on setting the application entry point (Main class) in your jar manifest file. 编辑:有关在jar清单文件中设置应用程序入口点(主类)的信息,请参阅使用清单文件:基础知识 。
Usually this happens when a dependent class (static member) is not found - like this, using log4j: 通常,当找不到依赖类(静态成员)时会发生这种情况 - 像这样,使用log4j:
public class MyClass {
private static Logger log = Logger.getLogger("com.example");
}
The reason is that the initialization of such a static member can be understood as part of the class loading - errors causing the class not to be available (loadable), resulting in the error you described. 原因是这样的静态成员的初始化可以被理解为类加载的一部分 - 导致类不可用(可加载)的错误,导致您描述的错误。
Static constructors are another possible reason: 静态构造函数是另一个可能的原因:
public class MyClass {
static {
// <b>any</b> error caused here will cause the class to
// not be loaded. Demonstrating with stupid typecast.
Object o = new String();
Integer i = (Integer) o;
}
}
I think others have covered some common stuff here. 我认为其他人已经介绍了一些常见的东西。 I'd jar tf the jar and make sure the class is listed.
我会在罐子里装瓶,并确保列出该类。 I'd also double-check that the class is public and the method is "public static void main(String[] arg)".
我还要仔细检查该类是否为public,方法是“public static void main(String [] arg)”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.