How can I run other programs using java program

hi experts,
I am trying to execute a set of commands from java program
eg: from command prompt
java CommandExecutor c:\winnt\notepad.exe c:\some.exe c:\onemorecommand.exe
and inside the CommandExecutor main method..
public static void main(String []arg)
for(i = 0; i < arg.length; i++)
Runtime.getRuntime().exec(arg(i));
the above code is executing all the command one after the other, but I want to execute the above commands squentially, i.e I want to execute the second command only after finishing the first command and the third after finishing the second and so on depending upon the arguments passed, how can I acheive this...
I have tried to use the Process which is returned by the exec(arg) method but the exitValue() returns same value before execution and after execution.
thanks,
krishna

Did you try to get the process returned and then try
process.waitFor() method. This waits for the process to finish.
After that try the next execution

Similar Messages

  • Please help How can i run Os commands thru Java programs

    Hey,
    I want to stop and restart the linux server thru java program.Is it possible to run the os commands thru java program.
    I had it thru Runtime.getRuntime().exec("*.exe");
    it only runs the exe files.How can run files other than exe files like .bat,com ans shell commands..Any body knows please help with the code..or mail to this address
    [email protected]
    thankyou,
    regards,
    j.mouli

    What about "start command.com /C execute.bat", or using the overload that takes a String[] as argument?
    What if you use the the full path of execute.bat?
    What error code do you get?
    And what comes th linux, I'm not sure... you'll need a shell interpreter there too, me thinks. (never had to run anything with runtime.exec on linux). Check the man pages if csh, bash, ksh, or what ever shell you like.

  • How can i get system variable using java

    Hi,
    I just want to know how can i get system variables using java code.
    for example i want to get the the date for today or i want to get the number of processes that's running.
    Thanks alot

    Hi,
    I just want to know how can i get system variables
    using java code.
    for example i want to get the the date for today or i
    want to get the number of processes that's running.
    Thanks alotSome generic "system variables" are available though Java, usually through the System class.
    Date today = new Date();
    is instantiated with the current date and time.
    Other system values, like environment values, should be passed to java through the command line (-D option) by setting system properties.
    Finally, platform specific values like the number of processes running will have to be written in platform specific code and executed by JNI (java native interface).
    Java is platform or system agnostic. Common system values, like time, are implemented. Hopefully you won't need platform specific values.

  • How can we prevent back button  using java script

    how can we prevent back button using java script

    Would be quicker for you to google for javaScript
    javascript:window.history.forward(-1);

  • FAQ: How can I invite others to use GroupPix?

    FAQ: How can I invite others to use GroupPix?
    Answer:
    GroupPix has two methods for adding others: 'Email invite' and 'Scan Pass'
    Email
    Email invitations work great before the event, because they allow others to install the app and join the Group Album ahead of time. There are two ways to invite someone via email. First, you need to go into the Group Album you created by tapping its icon from the main screen ('Grouppix party' is the example shown below):
    Then do one of the following:
    1. Tap the 'Add Friends' button in the lower right corner, then tap 'Invite with Email'
    2. Alternatively, you can tap the settings icon in the upper-right hand side to bring up the settings for the album, then tap 'Email Invite':
    Enter the email address of the person that you want to invite, then tap the blue invite button in the upper-right corner of the screen.
    If your friend does not have GroupPix already installed, the email contains a link to install the app from the App Store
    If you friend does have GroupPix already installed, the email has a link to launch the app, or your friend can just launch GroupPix without the email, and accept the invitation directly in the program.
    Scan Pass
    You can use the Scan Pass feature to add someone to an event on the spot. You (as the Group Album host) and the person you invite, each follows these directions, respectively:
    (You) Go into your Group Album, and tap the settings icon in the upper-right hand side to bring up the settings for the album, then tap 'Scan Pass' to switch GroupPix into Scan Mode:
    (Them) After installing the app from the App Store, launch the app, then tap 'Join' to show their GroupPix Pass:
    (You) Scan the other person's pass, carefully focusing on the square black and white code on the pass (circled in red above); once scanned, the other person will automatically be added to the Group Album, and photo taking can begin!

    If you are using icloud, make sure calendar is turned on.   Then you can invite others when you schedule an appointment.

  • How can I run other application?

    Hello everyone!
    I have few questions, for example I use this to run other application:
    Runtime.getRuntime().exec("C\\Program Files\\Opera\\Opera.exe");1. It works, but how can I run Opera.exe, when I don't know where was install Opera?
    2. I want open URL, but I don't care what browser will be use. How do this?
    p.s.
    I hope, you will understand my English =)

    In Windows, to open a URL in the default browser you simply use the START command, like this:START http://www.canadiantire.ca/Try that at the command line. You can use Runtime.exec() to do this, as has been discussed in several hundred earlier posts in this forum.

  • How to find Free disk space using Java program ?

    Hi friends,
    I need to find out the Free Hard disk Space to alert the user and send them mail if hard drive is about 85% full.
    i am developing databackup program in which i have to check it , if hard drive is fulled with 85% or remain left space is less email sent to user to notify and delete older backups.
    give me solution using java program...
    please
    Help me i need it in recent...

    Hi friends,
    I need to find out the Free Hard disk Space to alert
    the user and send them mail if hard drive is about
    85% full.
    i am developing databackup program in which i have to
    check it , if hard drive is fulled with 85% or remain
    left space is less email sent to user to notify and
    delete older backups.
    give me solution using java program...
    please
    Help me i need it in recent...

  • Can we run EXE file/ Another Java Program from Java Application? How?

    Can we run EXE file and another java program from java application?
    Thanks in advance

    Example running adobe acrobat
    String command = "C:\\Program Files\\Adobe\\Acrobat 5.0\\Reader\\AcroRd32.exe /t "+selectedDocument+" \\\\CONTROL\\HP LaserJet 4L";
    Runtime rn = Runtime.getRuntime();
    Process process = rn.exec(command);
    process.waitFor();rykk

  • How can i execute vb scripts in java program

    hi
    how can i execute any batch files or any other exe files (vb scripts) from java programs
    thanks

    Hi,
    You use Runtime.exec to execute commands / exe-files. See the documentation (and remember that it will only work on windows):
    http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Runtime.html
    /Kaj

  • How can I load an applet using Java Plug-in?

    I have written an applet and have tested with appletviewer, but I can't load it from a browser. Ghlin told me that I need to use Java Plug-in to load it. Please tell me how.

    Click on the Products and API link at the top left of this page. Download the Java Plugin. Be sure to get the developers version. Inside that developer's version there is a tool called HTMLConverter. Run HTMLConverter on your HTML and it will modify the applet tag in such a way that if the user already has the Java Plugin then it will use it. IF not, then the user will be asked if he wants to download it.

  • How Can we stop the execution of java program which contain infinite loop

    Dear All,
    I create one .bat file which which execute the simple java program
    I write one java program which execute above .bat file using instance of Runtime Class of java
    It's work fine if java program which is executed by .bat file is simple(Which has normal termination)
    But if that program contain inifinte loop then that file will goes on executing untill I close it manually
    So i want a solution in java which close that bat file (command window ) which is in running condtion
    If there any solution please replay me
    I try to close that .bat file using destroy() ,stop() method of Process and Runtime , Thread Class
    but it did not give me correct output
    .bat file still running !!!!!!!!!
    Thanks in advance

    Why not use javaw.exe to make the app run so you don't need the dos window (which is presumably what you are actually talking about when you say bat file).

  • How can i generate BARCODE Images using JAVA Code

    Hello ,
    My requirement is to generate BarCode Images to Print using JAVA Code.
    User will give only
    1) String.as Input Parameter and
    2) Type of Encoding you want to apply on to the String.
    outPut : code should generate Bar Code Images as per the requirements.
    can any one help me out with jars ????? or any link which can provide me start.

    1) I have requriement like generate doc file with
    more than one barcode image on that document.???
    So?
    If your chosen "doc file" format supports inclusion of multiple images there's no problem.
    If it doesn't, choose another format.
    2) I am able to generate .doc file but in which
    format i should open that doc file.????
    The same format you used to create it.
    3) Main thing is how can i put more than one barcode
    image on that doc file????
    That would depend on the file format and the API used to create it.
    is any one faced the same problem or gone through
    same requirement.
    Almost certainly. And they solved it too, given the document I'm looking at right now which is an e-ticket for an airline that has many barcodes and other generated images on it.
    So keep on trying, grasshopper, and you may find enlightenment.

  • How can I decode Japanese character using java from JavaScript escape() function

    Hi:
    I have an application which supports Japanese character. I am using JavaScript escape() function to encode the text field before I posted it through web server and then I will just convert it into hex number to decode it through an Integer. It works fine for the character between ascii 1-255. However, it faild on all the double bytes character. All the unicode has the format of "%uXXXX" after it is escaped from JavaScript. How can I resolve the decoding problem?
    Thank,
    David

    Please use 'encodeURI()', though it is supported only IE after 5.5.
    I'm not sure Netscape.
    Thanks,
    Katsumi
    dwang <[email protected]> wrote:
    Hi:
    I have an application which supports Japanese character. I am using JavaScript
    escape() function to encode the text field before I posted it through
    web server and then I will just convert it into hex number to decode
    it through an Integer. It works fine for the character between ascii
    1-255. However, it faild on all the double bytes character. All the
    unicode has the format of "%uXXXX" after it is escaped from JavaScript.
    How can I resolve the decoding problem?
    Thank,
    David

  • How can we open any file using JAVA...

    Hi all
    i trying to make code in that code i choose a file using a filechooser then put that file name in FILE object like (File file = fileChooser.getSelectedFile();)
    but the thing is how can i open that file for example if that file is HTML file then opens in IE or if that file is MS Word document that open in Word, like that
    is there any suggestions
    Thanx
    Regards
    Satinderjit

    start is a windows command-line utility.
    start foo.doc will start Word (or Star Office or Word Perfect or whatever is registered for the .doc extension),
    start bar.htmnl will start your registered browser etc.

  • Run shell commands using java program

    Hi guys,
    I am trying to run shell commands like cd /something and ./command with arguments as follows, but getting an exception that ./command not found.Instead of changing directory using "cd" command I am passing directory as an argument in rt,exec().
    String []cmd={"./command","arg1", "arg2", "arg3"};
    File file= new File("/path");
    try{
    Runtime rt = Runtime.getRuntime();
    Process proc = rt.exec(cmd,null,file);
    proc.waitFor();
    System.out.println(proc.exitValue())
    BufferedReader buf = new BufferedReader(new InputStreamReader(proc.getInputStream()));
    catch(Exception e)
    {e.printStackTrace();
    So can anyone please tell me what is wrong with this approach? or is there any better way to do this?
    Thanks,
    Hardik

    warnerja wrote:
    What gives you the idea that the process to execute is called "./command"? If this is in Windows, it is "cmd.exe" for example.It does not have to be cmd.exe in Windows. Any executable or .bat file can be executed as long as one either specifies the full path or the executable is in a directory that is in the PATH.
    On *nix the file has to have the executable bit set and one either specifies the full path or the executable must be in a directory that is in the PATH . If the executable is a script then if there is a hash-bang (#!) at the start of the first line then the rest of the line is taken as the interpreter  to use. For example #!/bin/bash or #!/usr/bin/perl .
    One both window and *nix one can exec() an interpreter directly and then pass the commands into the process stdin. The advantage of doing this is that one can change the environment in one line and it  remains in effect for subsequent line. A simple example of this for bash on Linux is
    import java.io.OutputStreamWriter;
    import java.io.Writer;
    public class ExecInputThroughStdin
        public static void main(String args[]) throws Exception
            final Process process = Runtime.getRuntime().exec("bash");
            new Thread(new PipeInputStreamToOutputStreamRunnable(process.getErrorStream(), System.err)).start();
            new Thread(new PipeInputStreamToOutputStreamRunnable(process.getInputStream(), System.out)).start();
            final Writer stdin = new OutputStreamWriter(process.getOutputStream());
            stdin.write("xemacs&\n");
            stdin.write("cd ~/work\n");
            stdin.write("dir\n");
            stdin.write("ls\n");
            stdin.write("gobbldygook\n"); // Forces output to stderr
            stdin.write("echo $PATH\n");
            stdin.write("pwd\n");
            stdin.write("df -k\n");
            stdin.write("ifconfig\n");
            stdin.write("echo $CWD\n");
            stdin.write("dir\n");
            stdin.write("cd ~/work/jlib\n");
            stdin.write("dir\n");
            stdin.write("cat /etc/bash.bashrc\n");
            stdin.close();
            final int exitVal = process.waitFor();
            System.out.println("Exit value: " + exitVal);
    }One can use the same approach with Windows using cmd.exe but then one must be aware of the syntactic differences between commands running in .bat file and command run from the command line. Reading 'help cmd' s essential here.
    The class PipeInputStreamToOutputStreamRunnable in the above example just copies an InputStream to an OutputStream and I use
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    public class PipeInputStreamToOutputStreamRunnable implements Runnable
        public PipeInputStreamToOutputStreamRunnable(InputStream is, OutputStream os)
            is_ = is;
            os_ = os;
        public void run()
            try
                final byte[] buffer = new byte[1024];
                for (int count = 0; (count = is_.read(buffer)) >= 0;)
                    os_.write(buffer, 0, count);
            } catch (IOException e)
                e.printStackTrace();
        private final InputStream is_;
        private final OutputStream os_;
    }

Maybe you are looking for