Good news! I think i’ve got it working, test it out here (it launches the BeanShell front-end app I’ve been working on): http://www.freewebs.com/commanderkeith/SlaveBot/SlaveBot.jnlp
I’d really appreciate it if people could try it out and let me know if it works (that is, that it starts at all and that after clicking ‘run’ the program automatically types ‘Hello World’).
I’d love some help improving this relaunching code, feel free to suggest imnprovements to the source. By the way I couldn’t get the Digibot program to work, probably that problem you mentioned.
Yeah the Slick games are really impressive. I just don’t know enough about OGL and all the associated troubles there are with drivers etc.
Thanks,
Keith
PS: 2 problems with the code at the moment are:
- The jar files are re-written on every invocation of the program. I do this since I want the jars to be up to date if the webstart jar was re-downloaded. Maybe there’s a more clever way to do it?
- The jar files are written to the user.home directory and there’s no fail-over if that directory is off-limits for whatever reason.
To use it, just dump your app’s jars in the same directory as wherever you put this class, then change the relevant parameters in the main method.
package wrapper;
import java.io.*;
import java.util.*;
import java.net.*;
/**
*
* @author woodwardk
*/
public class Main {
ArrayList<String> jarList = new ArrayList<String>();
File jarDirectory;
static final String fileSeparator = System.getProperty("file.separator");
/** Creates a new instance of Main */
public Main(File jarDirectory, ArrayList<String> jarList) {
this.jarList = jarList;
this.jarDirectory = jarDirectory;
}
public File buildJars() throws IOException{
/*if (jarDirectory.isDirectory() == false){
throw new IllegalArgumentException(jarDirectory.getAbsolutePath() + " doesn't exist!");
}*/
File mainJarFile = null;
for (String jarName : jarList){
// replace any '/' characters with the system's file separator.
// need to use quoteReplacement or else the windows '\' character behaves wierdly with replaceAll().
jarName = jarName.replaceAll("/", java.util.regex.Matcher.quoteReplacement(fileSeparator));
String jarFilePath = jarDirectory.getAbsolutePath()+fileSeparator+jarName;
File jarFile = new File(jarFilePath);
System.out.println(jarFilePath);
// save the file if its not already on the system
if (jarFile.isFile() == false){
jarFile.getParentFile().mkdirs();
jarFile.createNewFile();
}
// now have to switch the fileSearator back to the java-style '/' grrr....
jarName = jarName.replaceAll(java.util.regex.Matcher.quoteReplacement(fileSeparator), "/");
InputStream in = this.getClass().getResourceAsStream(jarName);
if (in == null){
throw new IllegalArgumentException(jarName + " wasn't found!");
}
BufferedInputStream bin = new BufferedInputStream(in);
byte[] bytes = new byte[bin.available()];
bin.read(bytes);
bin.close();
BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(jarFile));
bout.write(bytes);
bout.flush();
bout.close();
if (jarList.indexOf(jarName) == 0){
mainJarFile = jarFile;
}
}
return mainJarFile;
}
public void relaunchVM(File mainJarFile) throws IOException{
String javaExeName = "java";
File myJarFile = mainJarFile;
String jreJavaFilePath = System.getProperty("java.home") + fileSeparator + "bin" + fileSeparator + javaExeName;// +" ";
String jarOption = "-jar";//+" ";
//String classPathOption = "-classpath "+myJarFile.getParent() + fileSeparator; // don't need to specify classpath
ArrayList<String> commands = new ArrayList<String>();
commands.add(jreJavaFilePath);
commands.add(jarOption);
commands.add(myJarFile.getAbsolutePath());
System.out.println("About to launch new VM with the following commands:");
for (String aCommand : commands){
System.out.print(aCommand);
if (commands.indexOf(aCommand) != commands.size()-1){
System.out.print(" ");
}
}
System.out.println();
ProcessBuilder processBuilder = new ProcessBuilder(commands);
Process process = processBuilder.start();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
ArrayList<String> jarList = new ArrayList<String>();
jarList.add("SlaveBot.jar"); // the first one should contain the main-class.
jarList.add("lib/bsh-2.0b4.jar");
jarList.add("lib/jnlp.jar");
jarList.add("lib/substance.jar");
File jarDirectory = new File(System.getProperty("user.home")+"/SlaveBot");
Main main = new Main(jarDirectory, jarList);
try{
File mainJarFile = main.buildJars();
main.relaunchVM(mainJarFile);
}catch(IOException e){
e.printStackTrace();
}
System.exit(0);
}
}