Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在java程序中从不可运行的JAR调用类?_Java_Jar_Javafx - Fatal编程技术网

如何在java程序中从不可运行的JAR调用类?

如何在java程序中从不可运行的JAR调用类?,java,jar,javafx,Java,Jar,Javafx,考虑这一点-我有一个简单的单类应用程序,名为“TestApplication”(下面的源代码): JAR文件的附加结构: <archive = TestJar.jar> <folder = META-INF> <file = MANIFEST.MF> Manifest-Version: 1.0 </file> </folder> <file = .cl

考虑这一点-我有一个简单的单类应用程序,名为“TestApplication”(下面的源代码):

JAR文件的附加结构:

<archive = TestJar.jar>
    <folder = META-INF>
        <file = MANIFEST.MF>
            Manifest-Version: 1.0
        </file>
    </folder>
    <file = .classpath>
        <?xml version="1.0" encoding="UTF-8"?>
        <classpath>
            <classpathentry kind="src" path="src"/>
            <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
            <classpathentry kind="output" path="bin"/>
        </classpath>
    </file>
    <file = .project>
        <?xml version="1.0" encoding="UTF-8"?>
        <projectDescription>
            <name>TestJar</name>
            <comment></comment>
            <projects>
            </projects>
            <buildSpec>
                <buildCommand>
                    <name>org.eclipse.jdt.core.javabuilder</name>
                    <arguments>
                    </arguments>
                </buildCommand>
            </buildSpec>
            <natures>
                <nature>org.eclipse.jdt.core.javanature</nature>
            </natures>
        </projectDescription>   
    </file>
    <file = TestJar.class></file>
</archive>
调用ClassPathHack

接下来,我将他的解决方案与@Doswell在本主题中提出的建议合并在一起,如下所示:

button.setOnAction(new EventHandler<ActionEvent>() {
    @Override
    public void handle(ActionEvent ae) {
        try {
            ClassPathHack.addFile("C:/Users/John Smith/Desktop/TestJar.jar");

            Class<?> clazz = Class.forName("TestJar");
            Stage testJar = (Stage) clazz.newInstance();

        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } 
    }
})
button.setOnAction(新的EventHandler(){
@凌驾
公共无效句柄(ActionEvent ae){
试一试{
addFile(“C:/Users/John Smith/Desktop/TestJar.jar”);
Class clazz=Class.forName(“TestJar”);
Stage testJar=(Stage)clazz.newInstance();
}捕获(IOE异常){
e、 printStackTrace();
}catch(classnotfounde异常){
e、 printStackTrace();
}捕获(实例化异常e){
e、 printStackTrace();
}捕获(非法访问例外e){
e、 printStackTrace();
} 
}
})

一切都很完美。谢谢大家!

类加载器/安全性/正确的环境/正确的上下文问题除外:

  • TestJar.jar
    放在应用程序的类路径上
  • 从应用程序中调用

    /*TestJar ignored =*/ new TestJar();
    

  • 如果您希望从jar运行的类扩展/实现了您在代码中已经知道的某些东西,那么您可以使用反射来加载该类,但可以像您所知道的那样使用它,即ie javafx.stage.stage vs TestJar

    如果JAR已经在类路径上,那么您可以执行以下操作:

    Class<?> clazz = Class.forName("TestJar");
    Stage testJar = (Stage)clazz.newInstance();
    
    Class clazz=Class.forName(“TestJar”);
    Stage testJar=(Stage)clazz.newInstance();
    
    如果在类路径上没有JAR,但是有JAR的路径,那么可以遵循以下步骤
    然后按上述方式加载类

    将其添加到类路径并导入,然后构造它?不知道你在问什么。我已经编辑了我的问题以使其更具体。对不起,我错过了你的澄清(预览失败)。那么您想在运行时加载TestJar?如果是这样,您可以使用java.lang.Class.forName(“TestJar”)和java.Class.Class.newInstance()根据类的名称来实例化类(假设您的类加载器可以找到类并允许您拥有特权)
    /*TestJar ignored =*/ new TestJar();
    
    Class<?> clazz = Class.forName("TestJar");
    Stage testJar = (Stage)clazz.newInstance();