Solved

Java process execution - process seems to freeze during execution but works fine at command line

Posted on 2008-10-03
6
1,105 Views
Last Modified: 2013-11-25
I am launching a process from java (subversion client): however, it doesn't finish executing.

When I run the same process in the command line, the process successfully finishes.

I have also tried running a process to launch a .bat file for the subversion command, and I am stuck with the same result.  Yet, when I simply double click the same .bat file, as opposed to calling it from java, the process completes successfully.

Of further note is that, when I end the process with the Task Manager, I am given the result that I should get when the process finishes.
package com.floorsoft.floorwizard3.patcher;

 

import java.awt.Color;

import java.awt.Dimension;

import java.awt.Font;

import java.awt.Toolkit;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.border.LineBorder;

 

public class FW_Patcher

{

 

  public static final String ADDRESS     = "svn://*******************/fwx_production_repository";

 

 

  public static void main(String[] args)

  {

    try {

      String dirHome = System.getProperty("user.dir") +  "\\";

      JFrame frame = loadScreen();

      frame.setVisible(true);

 

      // ProcessBuilder

      ProcessBuilder pb = new ProcessBuilder("svn","co",ADDRESS,dirHome);

      pb.redirectErrorStream(true);

      Process prcUpdate = pb.start();

      prcUpdate.waitFor(); 

 

      // Attempt with Runtime.getRuntime().exec();

//      String[] commands = { "svn","co",ADDRESS,"." };

//      Process prcUpdate = Runtime.getRuntime().exec(commands);

//      prcUpdate.waitFor();

 

 

 

//      Process procUpdate = Runtime.getRuntime().exec(dirHOME + "update.bat");

//      procUpdate.waitFor();

 

 

 

        // an attempt to replace .waitFor()

//      while (true) {

//        try {

//          if (prcUpdate.exitValue() != 0) throw new IllegalStateException();

//          break;

//        } catch (IllegalThreadStateException itse) {

//          try { Thread.sleep(1000); }

//          catch (Exception e) {}

//        }

//      }

 

 

      frame.setVisible(false);

    } catch (Exception e) { e.printStackTrace(); }

  }

 

  private static JFrame loadScreen()

  {

    int loadWidth = 460;

    int loadHeight = 90;

    JFrame frmLoad = new JFrame();

    frmLoad.setBackground(Color.GRAY);

    frmLoad.setSize(loadWidth,loadHeight);

    frmLoad.setMinimumSize(new Dimension(loadWidth,loadHeight));

    frmLoad.setMaximumSize(new Dimension(loadWidth,loadHeight));

    frmLoad.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

    frmLoad.setResizable(false);

    frmLoad.setTitle("Updating...");

    frmLoad.setUndecorated(true);

    frmLoad.setFocusable(false);

    // Size & Position

    Dimension dimScreen = Toolkit.getDefaultToolkit().getScreenSize();

    int       screenX   = ((int)(dimScreen.getWidth()  / 2.0)) - (loadWidth / 2),

    screenY   = ((int)(dimScreen.getHeight() / 2.0)  - loadHeight / 2) - 20;

    frmLoad.setLocation(screenX,screenY);

    JLabel lblLoad = new JLabel("",JLabel.CENTER);

    lblLoad.setVerticalAlignment(JLabel.CENTER);

    lblLoad.setBounds(0,0,frmLoad.getBounds().x,frmLoad.getBounds().y);

    lblLoad.setFont(new Font("Arial",Font.BOLD,14));

    lblLoad.setText("Updating... Please Wait.");

    lblLoad.setBorder(new LineBorder(Color.BLACK,1));

    frmLoad.add(lblLoad);

    return frmLoad;

  }

 

}

Open in new window

0
Comment
Question by:modsiw
  • 3
  • 2
6 Comments
 
LVL 9

Expert Comment

by:mbodewes
ID: 22639013
Just calling setVisible(false) won't quit the Java application. You can use System.exit() for that. Swing launches threads of its own, and they will stay alive in the background until they are closed, or until the JVM exits. Just letting your own thread/application die won't do it. The rest of the commands will be executed as expected.
0
 
LVL 9

Expert Comment

by:mbodewes
ID: 22639023
That would be System.exit(0) for the correct error code (no error) of course. Disposing the frame will also work, but in this case I guess System.exit(0) is cleaner.
0
 
LVL 3

Author Comment

by:modsiw
ID: 22639026
I trimmed out a bunch of stuff to make the example of the issue clean.

When this runs, it doesn't get past line 28. The process normally completes in a couple seconds, but when called from java as above, it doesn't complete. Why?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 9

Accepted Solution

by:
mbodewes earned 500 total points
ID: 22639063
I just finished reading this:

http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html?page=2#resources

It seems you need to take care of the input/output streams. Just read everything away from the inputstream for starters. The application seems to be waiting for you to take its output.
0
 
LVL 5

Expert Comment

by:muktajindal
ID: 22648880
Rather than directly starting the process, try starting it in a thread. Create a class extending Thread class, override its run method to put the code to start the process. Then, simply instantiate the class and call thread.start().
0
 
LVL 3

Author Closing Comment

by:modsiw
ID: 31502897
Thanks man.  This was exactly what I was looking for.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
backtracking recursion  code 19 53
thymeleaf natural templating vs JSP 2 66
windows explorer path to command prompt 5 30
MySqlDump not dumping triggers 1 15
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now