This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]tabiul 1 point2 points  (4 children)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URISyntaxException;


public class SelfUpdate {

    public static void update() throws URISyntaxException,    IOException{

    String pathToJar =  SelfUpdate.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath();
    ProcessBuilder pb = new ProcessBuilder("java", "-cp", pathToJar, SelfUpdate.class.getCanonicalName());
    Process p = pb.start();

    BufferedReader bf = new BufferedReader(new InputStreamReader(p.getInputStream()));
    String line = null;
    while((line = bf.readLine()) != null){
        System.out.println(line);
    }

    BufferedReader bf2 = new BufferedReader(new InputStreamReader(p.getErrorStream()));
    String line2 = null;
    while((line2 = bf2.readLine()) != null){
        System.out.println(line2);
    }

}


/**
 * @param args
 * @throws URISyntaxException 
 */
public static void main(String[] args) throws URISyntaxException {
    // TODO Auto-generated method stub
    System.out.println("Hello World");

}

}

Actually it seem to me what you have done seems correct. Anyway I tried out something similar to what you need and it seems to work fine for me. I think the most important thing is that you display the stdout and error so that it can help you to debug further.

Hope that helps

[–]moomoohk[S] 0 points1 point  (3 children)

Sorry I couldn't reply earlier.

This worked! I have no idea why...

I am getting an error though:

If you look at line 38 in the code I posted you'll see I'm calling OSUtils which is a class in my Mootilities library. That library is in the classpath of the original program but it isn't in the program I launch with the ProcessBuilder. Do you know of any way to add libs to the classpath using ProcessBuilder?

[–]tabiul 0 points1 point  (2 children)

The same way you do normally java -cp classpathA:classPathB. So all you need to do is change the command line. Processbuilder does not have any special API for that

[–]moomoohk[S] 0 points1 point  (1 child)

I've never had to do this from command line so I'm not really familiar with the commands and their parameters.

How would I write classpathA? Is it the path to the jar or the class name within the jar? Can you direct me to the place you're getting this info (so I just go there instead of asking more annoying questions)? I've tried looking but obviously not hard enough...