无法使用外部类路径执行类

时间:2015-06-17 17:19:29

标签: java compilation openoffice.org

我有一个Test.java类

import com.sun.star.bridge.XUnoUrlResolver;
import com.sun.star.comp.helper.Bootstrap;
import com.sun.star.lang.XMultiComponentFactory;
import com.sun.star.lang.XMultiServiceFactory;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XComponentContext;

public class Test {



    XMultiServiceFactory ooConnect() {

        final String sConnectionString = "uno:socket,host=localhost,port=8100;urp;StarOffice.ServiceManager";
        // create the initial component context
        XComponentContext rComponentContext = null;
        try {
            rComponentContext = Bootstrap
                    .defaultBootstrap_InitialComponentContext();
        } catch (Exception e2) {
            // TODO Auto-generated catch block
            e2.printStackTrace();
        }

        // retrieve the servicemanager from the context
        XMultiComponentFactory rServiceManager = rComponentContext
                .getServiceManager();

        Object objectUrlResolver = null;
        try {
            objectUrlResolver = rServiceManager.createInstanceWithContext(
                    "com.sun.star.bridge.UnoUrlResolver", rComponentContext);
        } catch (Exception e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        // Create a new url resolver
        XUnoUrlResolver xurlresolver = (XUnoUrlResolver) UnoRuntime
                .queryInterface(XUnoUrlResolver.class, objectUrlResolver);

        XMultiServiceFactory rOfficeServiceManager = null;
        try {
            // resolve the uno-url
            Object objectInitial = xurlresolver.resolve(sConnectionString);

            rOfficeServiceManager = (XMultiServiceFactory) UnoRuntime
                    .queryInterface(XMultiServiceFactory.class, objectInitial);
        } catch (Exception e) {
            e.printStackTrace();
            return rOfficeServiceManager;
        }
        return rOfficeServiceManager;
    }




    public static void main (String [] args)

    {
        Test test = new Test();

        XMultiServiceFactory serviceFactory = test.ooConnect();
        if(serviceFactory != null)
        {
            System.out.println("YEYEYEYEY");
        }
    }
}

为什么我尝试编译这个我使用这个命令

javac -cp java_uno.jar;juh.jar;jurt.jar;ridl.jar;unoil.jar Test.java

它成功编译并给我上课。

但是当我尝试使用以下命令

执行该类时

java Test,我得到以下错误

Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/star/comp/helper/Bootstrap
        at Test.ooConnect(Test.java:18)
        at Test.main(Test.java:64)
Caused by: java.lang.ClassNotFoundException: com.sun.star.comp.helper.Bootstrap
        at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
        ... 2 more

或者如果我使用此命令

java -cp .:java_uno.jar;juh.jar;jurt.jar;ridl.jar;unoil.jar Test

我收到此错误 错误:无法找到或加载主类Test

任何帮助将不胜感激。

按照建议我将运行命令更改为

java -cp .;java_uno.jar;juh.jar;jurt.jar;ridl.jar;unoil.jar Test

现在我得到了这个例外

Exception in thread "main" java.lang.UnsatisfiedLinkError: no juh in java.library.path
        at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1886)
        at java.lang.Runtime.loadLibrary0(Runtime.java:849)
        at java.lang.System.loadLibrary(System.java:1088)
        at com.sun.star.lib.util.NativeLibraryLoader.loadLibrary(NativeLibraryLoader.java:65)
        at com.sun.star.comp.helper.Bootstrap.defaultBootstrap_InitialComponentContext(Bootstrap.java:199)
        at com.sun.star.comp.helper.Bootstrap.defaultBootstrap_InitialComponentContext(Bootstrap.java:167)
        at Test.ooConnect(Test.java:18)
        at Test.main(Test.java:64)

1 个答案:

答案 0 :(得分:1)

在Windows类中,类路径中的分隔符是分号(;)。对于UNIX,它是冒号(:)。您在一个命令行中使用两个分隔符:

java -cp .:java_uno.jar;juh.jar;jurt.jar;ridl.jar;unoil.jar Test

由于您的编译很好,我假设您使用的是Windows。因此,使用分号表示整个路径:

java -cp .;java_uno.jar;juh.jar;jurt.jar;ridl.jar;unoil.jar Test