Running command prompt using java

hi guys,
i am still quite new to programming. may i know how to run a bat file or command in the background of the application
without showing the command prompt.
right now i am able to do it, however it will still pop up the command prompt
the following is my coding.
try {
            String command = "cmd /C start C:/project/mkiosk/lib/createMovieJar.bat ";
            Runtime rt = Runtime.getRuntime();
            pr = rt.exec(command);
        } catch (IOException e) {
            e.printStackTrace();
        }

xclaim wrote:
...the following is my coding.
try {
Runtime rt = Runtime.getRuntime();
pr = rt.exec(command);
ProcessBuilder is a more robust way of creating a Process.
To avoid problems later, read and implement all the recommendations of [When Runtime.exec() won't|http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html] *(<- link).*
And as an aside, please put a single upper case letter at the start of every sentence. Adding the capital letter helps people to quickly scan the text, looking for ways to help. You would not want to make it harder to help, would you?

Similar Messages

  • Trying to find a way to execute from command prompt using java....

    I want to find a way to execute a command from the command line using a java program this is my code:
    import java.io.*;
    public class SQLHopefully
         public static void main(String[] args)
              try{
              Runtime rt = Runtime.getRuntime();
              Process proc = rt.exec("cmd /k start cmd.exe");
              OutputStream out = proc.getOutputStream();
              System.out.println(out);
              out.write("test.txt".getBytes()); //I THOUGHT THIS WOULD WORK
              InputStream stdin = proc.getErrorStream();
              InputStreamReader isr = new InputStreamReader(stdin);
              BufferedReader br = new BufferedReader(isr);
              String line = null;
              while ( (line = br.readLine()) != null) {
              System.out.println(line);
              stdin = proc.getInputStream();
              isr = new InputStreamReader(stdin);
              br = new BufferedReader(isr);
              line = null;
              while ( (line = br.readLine()) != null) {
              System.out.println(line);
              int exitVal;
              try {
              exitVal = proc.waitFor();
              } catch (InterruptedException e) {
              throw new IOException(e.getMessage());
              if (exitVal != 0) {
              throw new IOException("Exit value was " + exitVal ");
              }catch(IOException e)
              {System.out.println("ummmmm");}
    I hope my code is readable. I am trying to get the command prompt that comes up from the Runtime.exec() to run a file called "test.txt" I know it would be easier to just do it by executing it from java but I want to see if it is possible this way. I thought the out.write would do the trick but I was wrong. Somebody help please....
    Nate

    First read this article:
    http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html
    Second, you should probably send the Enter key to the shell if you want anything to happen. (Not that I know whether it will work, but I do know it won't work if you don't press Enter.)

  • Using command prompt through java, help please!

    Hi,
    What I am trying to do is find all the processes running onthe computer using java. Normally, I would do this (without java) by opening a command prompt window, typing in tasklist and see all the running processes. I have tried to to do the same thing using java, but so far have been unsucsessful. Here is my code:
    import java.io.*;
    public class Main {
            static DataOutputStream output;
            static DataInputStream input;
            static String app="cmd.exe";
            static String command="tasklist";
            static String message="";
            public static void main(String args[])throws Exception{
            Process process = Runtime.getRuntime().exec(app);
            output=new DataOutputStream(process.getOutputStream());
            input=new DataInputStream(process.getInputStream());
            output.write(command.getBytes());
            byte[] b=new byte[input.available()];
            input.readFully(b);
            message=new String(b);
            System.out.println("Message: "+message);
            System.out.println(!message.equals(""));
            System.out.println(message!=null);
            System.out.println("Finished Program");
    }And this program prints the output (with no exceptions):
    Message:
    false
    true
    Finished Program
    I appreciate any help in fixing my program...

    DevRocks_Java wrote:
    static String app="cmd.exe";
    static String command="tasklist";
    Process process = Runtime.getRuntime().exec(app);
    output=new DataOutputStream(process.getOutputStream());
    input=new DataInputStream(process.getInputStream());What do you expect it to do? You are only telling it to open a console window, nothing more.
    if you can type tasklist at the run option, then you should be able to just substitute command in for app in your exec.

  • Running programs WINDOWS using java

    Hi ,
    I would appreciate if anyone could tell me how to run programs WINDOWS using JAVA language .Thank you.

    I am not quite sure what you mean by that, but if you want to execute a program use:
    Runtime.exec(String)

  • Running command prompt externally from Java

    Hi,
    Does anyone know how to make a Java program that opens up the command prompt and runs certain commands? Thanks,
    Adam

    Read this before you use it:
    http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html

  • Using command prompt with Java

    I am trying to use command prompt within a java program and I can't figure out how to enter commands from java. I know how to start up command prompt but can someone guide me on a way to actually enter commands into the command prompt? Thanks.

    The Java� Tutorial - Lesson: Basic I/O
    ~

  • Running command prompt commands in java

    I can use:
    Process j = Runtime.getRuntime().exec("command";
    BufferedReader stdInput = new BufferedReader(new InputStreamReader(j.getInputStream()));
    BufferedReader stdError = new BufferedReader(new InputStreamReader(j.getErrorStream()));
    to run commands in the command line buf if i make another process..the command will not run...any ideas?

    ProcessBuilder can simplify things:
    redirectErrorStream
    <quote>
    If this property is true, then any error output generated by subprocesses subsequently started by this object's start() method will be merged with the standard output, so that both can be read using the Process.getInputStream() method. This makes it easier to correlate error messages with the corresponding output.
    </quote>

  • 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

  • Running batch files using java

    Hi,
    How can i run two batch files in a specified interval(Eg. 1 hour) using java.
    Thanks

    Hi,
    Learn to Use the API.
    import java.io.IOException;
    import java.util.Timer;
    import java.util.TimerTask;
    public class RunBat {
         static void runBatchFiles(String[] batchFiles)
              final String[] files = batchFiles;
              Timer timer = new Timer();
              TimerTask task = new TimerTask()          {               
                   Runtime r = Runtime.getRuntime();               
                   public void run() {
                        for(String command : files)
                             try {
                                  r.exec(command);
                             } catch (IOException e) {                              
                                  e.printStackTrace();
              timer.schedule(task, 1000, 1000*60*60);
         public static void main(String[] args) {
              runBatchFiles(new String[]{"cmd /C start f:/one.bat","cmd /C start f:/two.bat"});
    }Thanks

  • How to get no. of rows of Windows Command Prompt in java

    hi all,
    can anybody tell me that is there any java utility avalilable throght I can find the how many rows avalilbe on currently executing command prompt, and also how can i clear the content of command prompt (result of 'cls' command).
    Currently execting command prompt means from where I have run my java program.
    any reference is also welcome.
    thanks in advance.

    Clearing the screen is trivial, as long as you're OK with making your program specific to Windows only (i.e. you never hope to run it on any other OS ever):
    Runtime.getRuntime().exec("cmd /c cls");As to figuring out how many lines your command window's screen buffer has - good luck with that. I'm not familiar enough with the Win32 API to get this info, but you'll have to figure out what that is, and call it via JNI.

  • Help! how to run other programs using java

    how can i run other programs or exe files using java, anyone can give sample codes for this.
    ex. if I click [run MsWord] button, then msWord automatically lunch

    RunTime.getRuntime().exec(string command)
    I guess, it might help you..java.lang.Runtime

  • Unable to run my application using java web start

    i have created one application and use java web start to download the application. all the files are succesfully downloaded but when i try to run the application an expected error occur. this only happen in my machine but not in other machine i'm currently using
    -jsdk1.4.2
    -windows 2000 professional 5.00.2195 service pack 3
    -pentium III 870 Mhz
    -x86-based PC
    -RAM 256 Mhz
    Anyone knows why, plz help me with this puzzle

    Please post the information from the Web Start "Exception" and "Wrapped Exception" tabs when the error occurs (I assume the error was in fact unexpected ;-)
    A blind stab in the dark would be that you don't have write permission to the cache directory on the second machine. Also worth checking that the proxy settings on the second machine are the same as the first (if they are on the same network).

  • Pass the text file name to the command prompt using servlet

    hi to all,
    I have a HTML page that takes the user inputs. Once the user clicks on the submit button on the HTML page, these values will be capture by a servlet and then store into a text file. A new text file is generated each time the user clicks on the submit button.
    Now, i need to call a command prompt window and then pass the text file name to another program for execution. Any idea how i can pass the text file name to the command prompt???
    Thanx a lot....
    kinki

    hi, thanx 4 ur reply.
    But i not onli want to display the file name on the Tomcat command prompt. I must also able to call up a program to execute the command. If i used System.out.println(); it onli display the value on the console windows. But not executing it... anyway where i can do it???.
    thanx...
    Kinki

  • Batch command for using Java in IE

    Hello,
    I'm looking for a batch command that sets IE to use Java (Sun).
    It should do the same job as if manually checking the check box of 'Use <java version> for applet' in advanced internet options of IE.
    Is there a way to do it?

    Sorry, currently there's no batch command to set IE to use Java from Sun.
    There're quite a few registry keys need to be set when that option is selected.
    Registry settings for:
    - turning off console for MSVM;
    - hooking up Sun's Java console to IE;
    - adding a "TreatAs" to the Web Browser Applet Control so that applets with applet tag will use Sun's Java;
    etc.

  • Running the client using "java"

    Hi,
    This is the error I get
    jeevak@scooby(75)%!8
    java -classpath ./dist/hello-client.jar hello.HelloClient jeevak
    Exception in thread "main" java.lang.NoClassDefFoundError: javax/xml/rpc/Stub
    I looked up jaxrpc-api.jar and it includes this class and have it in my $CLASSPATH variable.
    jeevak@scooby(76)%echo $CLASSPATH
    .:/home/g3/users/jeevak/jdom-b8/build/jdom.jar:
    /home/g3/users/jeevak/Dotstuff/xerces-1_4_4/xerces.jar:
    /home/g3/users/jeevak/Dotstuff/xerces-1_4_4/xercesSamples.jar:
    /usr/local/java/cog-0.9.13/build/cog-0.9.13/lib/cog.jar:
    /usr/local/java/cog-0.9.13/build/cog-0.9.13/lib/log4j-core.jar:
    /home/g3/users/jeevak/j2sdk1.4.1/lib/security/local_policy.jar:
    /home/g3/users/jeevak/j2sdk1.4.1/lib/security/US_export_policy.jar:
    /home/g3/users/jeevak/jwsdp-1_0_01/common/lib/jaxrpc-api.jar
    The run is successful using ant run.
    I tried to figure out the classpath ant uses but have no access to the source code for AntExecutable.java.
    Any ideas?
    -jeevak

    Try to add in your classpath all the following jar files :
    d:\jwsdp\common\lib\jaxrpc-api.jar;d:\jwsdp\common\lib\jaxrpc-ri.jar;d:\jwsdp\common\lib\activation.jar;d:\jwsdp\common\lib\mail.jar;d:\jwsdp\common\lib\jaxp-api.jar;d:\jwsdp\common\lib\saaj-api.jar;d:\jwsdp\common\lib\saaj-ri.jar;d:\jwsdp\common\endorsed\sax.jar;d:\jwsdp\common\endorsed\dom.jar;d:\jwsdp\common\endorsed\crimson.jar

Maybe you are looking for

  • FCP w/ Intensity Pro for monitoring

    Disclaimer: before someone points me to www.blackmagic-design.com/products/intensity/ .. yes, I have already looked there. I would like opinions on the card more than hard specs .. My main concern at the moment is not ingest, but monitoring. I'm look

  • Can I change my email in my apple ID?

    I want to know if I can change my email in the apple ID settings. Will I lose my apps if I do? Does it take any specific steps?

  • URGENT: Need to change my access number (moved to ...

    I had a US access number.  I now live in France and need a French access number.  How can I do that?  I need to make this change very quickly (within next 45 min, if at all possible). Thanks. Sophie

  • *URGENT* Firmware Problem On Zen To

    Hey, Ok so i was updating my firmware using the latest and legit download from your website when an error occured. Now whenever i boot up my ZEN TOUCH i get "Firmware Problem" and it goes into recovery mode. I have tried updating on 2 different compu

  • Adobe Flash Palyer for Nokia E50

    Good Day, Does anyone know how to get the Adobe Mobile flash player on a E50 cellphone.