简体   繁体   English

从jar文件中运行特定的类main函数。导入似乎不起作用

[英]running specific class main function from jar file. import seems not working

I have a jar file without its main class specified in manifest. 我有一个jar文件没有在manifest中指定它的主类。 So i followed an answer given here: 所以我按照这里给出的答案:

How to run a class from Jar which is not the Main-Class in its Manifest file 如何从Jar运行一个类,它不是Manifest文件中的Main-Class

It seems to try to run main from this class. 它似乎试图从这个类运行main。 However it looks like importing some other class from this jar file is broken for some reason. 但是,由于某种原因,从这个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$1.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如何才能运行?

The exception indicates that you need to add some other JARs into the classpath. 该异常表示您需要将其他一些JAR添加到类路径中。 Classes in your test.jar depend on other classes. test.jar中的类依赖于其他类。 eg on com.ibm.OS4690.FlexosException. 例如,在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. 您可以尝试搜索另一个JAR文件(在您的test.jar所在的位置),以便它包含FlexosException.class文件。 Once you find it, run your test.jar as 找到后,运行test.jar

java -cp test.jar;<path_to_another_jar_here> ClassIWantToRun

You won't be able to run your program outside an OS4690 environment because you're depending on internal OS4690 libraries. 您将无法在OS4690环境之外运行程序,因为您依赖于内部OS4690库。 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. 如果您可以访问OS4690安装,您可能会找到所需的jar,但最后这些jar使用平台相关库。 Try to avoid using those dependencies if you're not developing for that specific platform. 如果您没有针对该特定平台进行开发,请尽量避免使用这些依赖项。

java -cp test.jar ClassIWantToRun

Is importing the JAR containing the class you want to run. 导入包含要运行的类的JAR。 You should as well import the JAR that includes ClassA on your classpath. 您还应该在类路径中导入包含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需要在你的类路径上

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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