java 从 jar 文件运行特定的类 main 函数。导入似乎不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14400176/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
running specific class main function from jar file. import seems not working
提问by fsw
I have a jar file without its main class specified in manifest. So i followed an answer given here:
我有一个没有在清单中指定主类的 jar 文件。所以我按照这里给出的答案:
How to run a class from Jar which is not the Main-Class in its Manifest file
It seems to try to run main from this class. However it looks like importing some other class from this jar file is broken for some reason.
它似乎试图从这个类运行 main 。但是,由于某种原因,从这个 jar 文件中导入一些其他类似乎被破坏了。
Here is the minimized version of my problem:
这是我的问题的最小化版本:
jar tf test.jar
gives:
给出:
META-INF/
META-INF/MANIFEST.MF
ClassIWantToRun.class
something/
something/something/
something/something/something/ClassA.class
Sources of ClassIWantToRun.class viewed with jd-gui seems to be:
使用 jd-gui 查看的 ClassIWantToRun.class 的来源似乎是:
import something.something.something.ClassA;
public class ClassIWantToRun
{
public static void main(String[] args)
{
int x = ClassA.comeMethod();
}
}
Running this with:
运行这个:
java -cp test.jar ClassIWantToRun
gives me the exception:
给我一个例外:
Exception in thread "main" java.lang.NoClassDefFoundError: com/ibm/OS4690/FlexosException
at ClassIWantToRun.main(ClassIWantToRun.java:7)
Caused by: java.lang.ClassNotFoundException: com.ibm.OS4690.FlexosException
at java.net.URLClassLoader.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
... 1 more
I know only basics of Java but it seems that ClassA can not be found even with the line: import something.something.something.ClassA How can i make this run?
我只知道 Java 的基础知识,但似乎即使使用以下行也找不到 ClassA:import something.something.something.ClassA 我怎样才能运行它?
回答by maksim_khokhlov
The exception indicates that you need to add some other JARs into the classpath. Classes in your test.jar depend on other classes. e.g. on com.ibm.OS4690.FlexosException.
该异常表明您需要将其他一些 JAR 添加到类路径中。test.jar 中的类依赖于其他类。例如在 com.ibm.OS4690.FlexosException 上。
You can try searching for another JAR file (in the same place you took your test.jar) so that it will contain the FlexosException.class file. Once you find it, run your test.jar as
您可以尝试搜索另一个 JAR 文件(在您获取 test.jar 的同一位置),以便它包含 FlexosException.class 文件。一旦你找到它,运行你的 test.jar 作为
java -cp test.jar;<path_to_another_jar_here> ClassIWantToRun
回答by Daniel
You won't be able to run your program outside an OS4690 environment because you're depending on internal OS4690 libraries. You might find the jar you need if you have access to an OS4690 installation, but at the end those jars use platform dependant libraries. Try to avoid using those dependencies if you're not developing for that specific platform.
您将无法在 OS4690 环境之外运行您的程序,因为您依赖于内部 OS4690 库。如果您有权访问 OS4690 安装,您可能会找到所需的 jar,但最终这些 jar 使用平台相关库。如果您不是为特定平台开发,请尽量避免使用这些依赖项。
回答by cowls
java -cp test.jar ClassIWantToRun
Is importing the JAR containing the class you want to run. You should as well import the JAR that includes ClassA on your classpath.
正在导入包含要运行的类的 JAR。您还应该在类路径中导入包含 ClassA 的 JAR。
In your case, I guess it is the JAR that contains com/ibm/OS4690/FlexosException
that needs to be on your classpath
在你的情况下,我猜它是包含com/ibm/OS4690/FlexosException
需要在你的类路径上的 JAR