Results 1 to 3 of 3
  1. #1
    mfurtado is offline Member
    Join Date
    Jul 2009
    Posts
    1
    Rep Power
    0

    Default Executing a sh file from java program in foreground

    Hi

    My purpose is to restart my Java application from within the application.
    For this I have created a Java program that runs a sh file. This sh file has the command to start the same java Program.
    when i run the parent Java program it executes the sh file and runs the Java Program in it in background. I want this program to run in foreground since the program takes inputs from the user. How can i make the program run in foreground.

    Following is the code i use to execute the sh file

    Java Code:
      
    
    String LINUX_CLIENT_BATCH_FILE = "client.sh";
    String command = "./" + LINUX_CLIENT_BATCH_FILE;
    Process theProcess=Runtime.getRuntime().exec(command);
    System.exit(0);
    Following is sh file

    Java Code:
       
    
    #!/bin/bash -e
    
    rm -rf sdt-socket-client.log*
    cp=".:conf:"
    
    for lib in `find -name \*.jar`
    do
    	cp="$cp:$lib"
    done
    if [ $1 ]; then
    java -XX:NewSize=128M -XX:MaxNewSize=128M -XX:SurvivorRatio=8 -Xms350M -Xmx350M -Xrs -cp $cp com.sdt.A.D 'conf/' "localhost" 4445 'db/' $1 $2 $3 & echo $! > /usr/local/srb/srb_java.pid
    else 
    java -XX:NewSize=128M -XX:MaxNewSize=128M -XX:SurvivorRatio=8 -Xms350M -Xmx350M -Xrs -cp $cp com.sdt.A.D 'conf/' "localhost" 4445 'db/'
    fi
    Thanks

  2. #2
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    The main question: Why?
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  3. #3
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    I play around w/ a similar code on Windows. What happens is that Runtime.exec creates a new process, so you'll need to get the input/output streams and display it in the current shell. Otherwise it just runs in the background.

    And also, "theProcess" needs to call waitFor() to wait for the shellscript to finish before exiting.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

Similar Threads

  1. Replies: 3
    Last Post: 04-02-2009, 09:16 PM
  2. Executing java program using windows scheduler
    By kanitha in forum New To Java
    Replies: 0
    Last Post: 03-21-2009, 10:16 AM
  3. Replies: 2
    Last Post: 02-10-2009, 03:37 PM
  4. Executing an external java program
    By arunsubramanian in forum Advanced Java
    Replies: 5
    Last Post: 02-06-2009, 08:49 AM
  5. executing the java file
    By TheBayWatchMan in forum New To Java
    Replies: 4
    Last Post: 01-12-2008, 10:31 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •