Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Implementing restartApplication method in Java

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Implementing restartApplication method in Java

    I attempted to implement my own version of a restartApplication method I found on the internet. All I added was the location of my javabin and the location of a jar file for it to relaunch. I also made the method static so I don't have to create an object to call it.

    When I run my program, it runs fine up until the point it is supposed to restart. Instead of restarting, I get this error:

    java.io.IOException: Cannot run program "null\bin\java": CreateProcess error=2, The system cannot find the file specified
    at java.lang.ProcessBuilder.start(ProcessBuilder.java :1029)
    at TreeChartUI.restartApplication(TreeChartUI.java:54 3)
    at TreeChartUI.main(TreeChartUI.java:596)
    Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
    at java.lang.ProcessImpl.create(Native Method)
    at java.lang.ProcessImpl.<init>(ProcessImpl.java:189)
    at java.lang.ProcessImpl.start(ProcessImpl.java:133)
    at java.lang.ProcessBuilder.start(ProcessBuilder.java :1021)
    ... 2 more


    Here is my attempt at the restart method.

    public static void restartApplication()
    {
        final String javaBin = System.getProperty("C:\\Program Files\\Java\\jdk1.7.0_05\\bin") + File.separator + "bin" + File.separator + "java";
        final File currentJar = new File("C:\\Somefolder\\someJar.jar");//new File(UpdateReportElements.class.getProtectionDomain().getCodeSource().getLocation().toURI());
     
      /* is it a jar file? */
        if(!currentJar.getName().endsWith(".jar"))
        return;
     
      /* Build command: java -jar application.jar */
        final ArrayList<String> command = new ArrayList<String>();
        command.add(javaBin);
        command.add("-jar");
        command.add(currentJar.getPath());
     
        final ProcessBuilder builder = new ProcessBuilder(command);
        try{
        builder.start();
        }
        catch (IOException e) {
     
                      // TODO Auto-generated catch block
     
                      e.printStackTrace();
     
                }
        System.exit(0);
        }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Implementing restartApplication method in Java

    Does your computer have the fule path "null\bin\java"? It looks to me something is null in the construction of the path, I'd bet it has to do with the attempt to get the System property....see the list of properties you can access
    System Properties (The Java™ Tutorials > Essential Classes > The Platform Environment)

  3. The Following User Says Thank You to copeg For This Useful Post:

    davidvee (August 10th, 2012)

  4. #3
    Junior Member
    Join Date
    Jul 2012
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Implementing restartApplication method in Java

    Quote Originally Posted by copeg View Post
    Does your computer have the fule path "null\bin\java"? It looks to me something is null in the construction of the path, I'd bet it has to do with the attempt to get the System property....see the list of properties you can access
    System Properties (The Java™ Tutorials > Essential Classes > The Platform Environment)
    I'm not sure what you mean by having the full path. Would it be possible to avoid the getProperty method alltogether by just giving it the String path of my javaBin? I tried changing the line from

    final String javavBin = System.getProperty("C:\\Program Files\\Java\\jdk1.7.0_05\\bin") + File.separator + "bin" + File.separator + "java";

    To
        final String javaBin = "C:\\Program Files\\Java\\jdk1.7.0_05\\bin";

    But I got this error. I don't see why my access would be denied.

    java.io.IOException: Cannot run program "C:\Program Files\Java\jdk1.7.0_05\bin": CreateProcess error=5, Access is denied
    at java.lang.ProcessBuilder.start(ProcessBuilder.java :1029)
    at TreeChartUI.restartApplication(TreeChartUI.java:54 3)
    at TreeChartUI.main(TreeChartUI.java:596)
    Caused by: java.io.IOException: CreateProcess error=5, Access is denied
    at java.lang.ProcessImpl.create(Native Method)
    at java.lang.ProcessImpl.<init>(ProcessImpl.java:189)
    at java.lang.ProcessImpl.start(ProcessImpl.java:133)
    at java.lang.ProcessBuilder.start(ProcessBuilder.java :1021)
    ... 2 more
    Last edited by davidvee; August 10th, 2012 at 10:17 AM.

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Implementing restartApplication method in Java

    java.io.IOException: Cannot run program "C:\Program Files\Java\jdk1.7.0_05\bin"
    The path looks like it's to a folder NOT to an executable file. Should there be a command (like java.exe) at the end of the path?

    To see what system properties might be useful try this
    		Properties props = System.getProperties();
    		props.list(System.out);
    Last edited by Norm; August 10th, 2012 at 11:05 AM.
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    davidvee (August 10th, 2012)

Similar Threads

  1. Implementing the compareTo method?
    By colerelm in forum Java Theory & Questions
    Replies: 2
    Last Post: December 3rd, 2011, 07:47 PM
  2. Replies: 1
    Last Post: September 15th, 2011, 07:50 AM
  3. Implementing order of operations for a Java calculator.
    By mjpam in forum Java Theory & Questions
    Replies: 2
    Last Post: May 15th, 2011, 06:11 PM
  4. Implementing HTML tags in Java Source Code
    By bookface in forum Java Theory & Questions
    Replies: 4
    Last Post: March 19th, 2010, 09:29 PM
  5. Replies: 6
    Last Post: October 20th, 2009, 06:33 AM