簡體   English   中英

如何在Java程序中從不可運行的JAR調用類?

[英]How to call a class from non-runnable JAR in java program?

考慮一下-我有一個簡單的一類應用程序,名為“ TestApplication”(下面的源代碼):

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class TestApplication extends Application {

    public Parent createContent() {

        /*layout*/
        BorderPane layout = new BorderPane();

        /*layout -> center*/
        Button button = new Button("Run JAR");
        button.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent ae) {
                // Call TestJar.class from TestJar.jar
            }
        });

        /*add item to the layout*/
        layout.setCenter(button);
        return layout;
    }

    @Override
    public void start(Stage stage) throws Exception {
        stage.setScene(new Scene(createContent()));
        stage.setWidth(200);
        stage.setHeight(200);
        stage.show();
    }

    public static void main(String args[]) {
        launch(args);
    }
}

現在,我想在其button.setOnAction()方法中調用“ TestJar.class”(擴展了javafx.stage.Stage),這是不可運行的“ TestJar.jar”存檔的唯一類。 但是問題是,我希望能夠從“ TestApplication”級別調用“ TestJar.class”,而無需事先在代碼中添加導入。 例如,我有一個應用程序,為此我提供了jar的位置以及有關此jar中的類名稱的信息,這些信息應被調出。 下面的“ TestJar.java”類的代碼:

import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class TestJar extends Stage {

    public TestJar() {
        super();
        BorderPane layout = new BorderPane();
        Label label = new Label("This is test");
        layout.setCenter(label);
        Scene scene = new Scene(layout, 200, 200);
        this.setScene(scene);
        this.show();
    }
}

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>

我可以通過“ TestApplication”類以編程方式調用該“ TestJar”類嗎?

更新2014/07 / 26,11:37AM

我使用了@Allain Lalonde編寫的示例,該示例如何在運行時動態加載Jars? 稱為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();
        } 
    }
})

一切正常。 謝謝!

如果您想從jar中運行的類已經擴展/實現,您已經在代碼中了解到一些內容,則可以使用反射來加載該類,但是可以像您所知道的那樣使用它來工作,例如javafx.stage.Stage與TestJar。

如果JAR已經在類路徑中,則可以執行;否則,請執行以下步驟。

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

如果您在類路徑上沒有JAR,但是有jar的路徑,則可以按照以下步驟進行操作: 我應該如何加載jar ,然后按上述方式加載類

除了Classloader / security / correct-environment / correct-context以外,還需要注意以下事項:

  1. TestJar.jar放在應用程序的類路徑上
  2. 從您的應用程序中,調用

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM