简体   繁体   中英

Load a class with arguments within the same project

Alright, i'm trying to Xbootclasspath a jar from within my project. Currently I have to load my application through command-line with the follow command:

java -Xbootclasspath/p:canvas.jar -jar application.jar

This works perfectly fine but I want to do this without having to enter command line, is there I way I can Xbootclasspath from within the jar?

Thanks.

The most clear solution is to have two main classes.

Your first class, named Boot or similar, will be the outside entry point into the application, as set in the jar's manifest. This class will form the necessary runtime command to start your actual main class (named Application or similar), with the Xboot parameter.

public class Boot {

    public static void main(String[] args) {
        String location = Boot.class.getProtectionDomain().getCodeSource().getLocation().getPath();
        location = URLDecoder.decode(location, "UTF-8").replaceAll("\\\\", "/");
        String app = Application.class.getCanonicalName();
        String flags = "-Xbootclasspath/p:canvas.jar";
        boolean windows = System.getProperty("os.name").contains("Win");

        StringBuilder command = new StringBuilder(64);
        if (windows) {
            command.append("javaw");
        } else {
            command.append("java");
        }
        command.append(' ').append(flags).append(' ');
        command.append('"').append(location).append('"');
        // append any necessary external libraries here
        for (String arg : args) {
             command.append(' ').append('"').append(arg).append('"');
        }

        Process application = null;
        Runtime runtime = Runtime.getRuntime();
        if (windows) {
            application = runtime.exec(command.toString());
        } else {
            application = runtime.exec(new String[]{ "/bin/sh", "-c", command.toString() });
        }

        // wire command line output to Boot to output it correctly
        BufferedReader strerr = new BufferedReader(new InputStreamReader(application.getErrorStream()));
        BufferedReader strin = new BufferedReader(new InputStreamReader(application.getInputStream()));
        while (isRunning(application)) {
            String err = null;
            while ((err = strerr.readLine()) != null) {
                System.err.println(err);
            }
            String in = null;
            while ((in = strin.readLine()) != null) {
                System.out.println(in);
            }
            try {
                Thread.sleep(50);
            } catch (InterruptedException ignored) {
            }
        }
    }

    private static boolean isRunning(Process process) {
        try {
            process.exitValue();
        } catch (IllegalThreadStateException e) {
            return true;
        }
        return false;
    }
}

And your Application class runs your actual program:

public class Application {

    public static void main(String[] args) {
        // display user-interface, etc
    }
}

感觉令人讨厌,但是您可以执行Runtime.exec来使用提供的选项和新参数(以及一些寻找该条件的条件代码)调用java来防止产生新进程的递归循环吗?

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