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: how best to run external process and control it

  1. #1
    Junior Member
    Join Date
    Aug 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default how best to run external process and control it

    I have a Java standalone gui developed using eclipse Java EE.

    Firstly I need to run a C executable with the command line similar to (it varies based on user input) this:

    String filePath = "nohup C:/Users/User/workspacejavatest2/t3drive.exe -v4 fred777 1 1 1 java2.msg > fred777.log 2>&1 &";
    Process p = Runtime.getRuntime().exec(filePath);

    First question is how best to ensure this is correctly escaped. Is there a uri type approach or is it a manual job?

    Secondly I'd like to kill the process if something goes wrong. What is the best approach when this might be used on Linux, Windows, or whatever?

    Just looking for guidance, not code, as don't want to do it the longway round if there is a slick approach. Many thanks for viewing and even more tx for any ideas


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: how best to run external process and control it

    You might want to use ProcessBuilder.

    What exactly are you trying to escape?

    The Process class has a destroy() method, as well as a destroyForcibly() method as of Java 8.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    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: how best to run external process and control it

    Valuable information can be found at When Runtime.exec() won't | JavaWorld

    As Kevin mentioned, for more complex command calls ProcessBuilder might help. Redirects and pipes directly within the command using Runtime exec are not reliable - one should get the input and error streams directly, read them in separate threads, and do what is necessary with the result (write to file, provide to user, etc...). ProcessBuilder provide convenient methods to accomplish this (eg redirectOutput). For more complex commands - for instance ones which require pipes between multiple tools - the 'headache' step can often be skipped by writing a script file (batch or bash) containing the command(s) and calling that script with a single runtime.exec/ProcessBuilder call. Also see the API for the Process class, which contains important methods

  4. #4
    Junior Member
    Join Date
    Aug 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: how best to run external process and control it

    Thanks Kevin and Copeg. Very useful stuff and you have me off in the right direction. Already I can see that I can do what is required but just need to build a few examples and get familiar with it. Already have ProcessBuilder in a test environment and having fun. OK only dir todate but we all start somewhere.

    As you say Copeg we can always shell to avoid pipes, and when not on our beloved linux we have cygwin so no need for any Windows stuff.

    Cheers

Similar Threads

  1. How to copy external jars in ext folder of jre to access these at run time by applet.
    By zsanchit@gmail.com in forum What's Wrong With My Code?
    Replies: 0
    Last Post: July 3rd, 2014, 08:29 AM
  2. Replies: 2
    Last Post: November 18th, 2012, 02:09 PM
  3. run an external program
    By righi in forum AWT / Java Swing
    Replies: 4
    Last Post: June 21st, 2012, 09:28 AM
  4. Help with non-blocking thread that runs an external process
    By rkad40 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 9th, 2011, 04:27 PM
  5. process control
    By ttsdinesh in forum Java Native Interface
    Replies: 6
    Last Post: October 27th, 2009, 06:29 PM

Tags for this Thread