java - Load a class with arguments within the same project -
alright, i'm trying xbootclasspath jar within project. have load application through command-line follow command:
java -xbootclasspath/p:canvas.jar -jar application.jar
this works fine want without having enter command line, there way can xbootclasspath within jar?
thanks.
the clear solution have two main classes.
your first class, named boot
or similar, outside entry point application, set in jar's manifest. class form necessary runtime command start actual main class (named application
or similar), 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 necessary external libraries here (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 boot output 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 application
class runs actual program:
public class application { public static void main(string[] args) { // display user-interface, etc } }
Comments
Post a Comment