简体   繁体   中英

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

I have a jar file without its main class specified in 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

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.

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:

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?

The exception indicates that you need to add some other JARs into the classpath. Classes in your test.jar depend on other classes. eg on 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

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. 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.

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.

In your case, I guess it is the JAR that contains com/ibm/OS4690/FlexosException that needs to be on your classpath

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