Command window and Runtime.getRuntime()

Hi,
I am trying to execute Runtime.getRuntime().exec() command on click of a button. It works fine, but the problem is that i get a command window when the command is executed. Is there any way to make the command window not to appear?

It depends on the operating system you hava. Look at the os's help information on the command (or cmd) and start commands. Using "start /b" in the exec string may work.

Similar Messages

  • Open a command window and send some keystroke to the opened command window

    Hi All,
    I am trying to open command prompt and send some key stroke to that command prompt. Here I am using Robot to send keystroke. But the problem is, the keystrokes are not sent to the prompt. I guess I need to activate progamitically the command prompt to accept the keystrokes . Please find the code below ( as I did).
    try
    String command = "cmd.exe /c start";
    Process child = Runtime.getRuntime().exec(command);
    Robot robot = new Robot();
    Thread.sleep(5000);
    String lol = "indranil";
    for(int i=0;i<lol.length();i++)
    robot.keyPress(lol.charAt(i));
    int g = lol.length() - 1;
    robot.keyRelease(lol.charAt(g));
    catch (Exception e) {}
    I guess I am doing some mistake here while sending the keystroke/openning the command prompt. Can anyone please help me how can I open a command window and send some keystroke to the opened command window. I am in fix.
    Thanks,
    Indranil

    The first thing to do is read the API and examples for the Robot class.
    Also check out this thread with a similar discussion: [http://forums.sun.com/thread.jspa?threadID=5385677]
    The short spoonfed answer - keyPress() and keyReleaseI() do not take chars. Read up on KeyEvent

  • Difference between command line and Runtime.exec()

    Hi all.
    I'm coding some lines to call sqlldr program.
    System info:
    OS: Win2k server
    Java platform: JSK 1.4
    DBMS: Oracle 9i
    I've tested sqlldr in command line and it is OK. But when i call Runtime.exec("sqlldr user/pass@servicename control=mycontrol.ctl")
    the ErrorStream show that: Message 2100 not found,No message file for product=RDBMS ,facility=UL...
    So i had to put a String array which contained "ORACLE_HOME" as the second parameter of exec method. But there's another error appear:
    SQL*Loader-704: Internal error: ulconnect: OCIServerAttach [0]
    ORA-12560: TNS:protocol adapter error
    I checked tnsnames.ora and it's OK. I do the command line again and it's still OK. Why did Runtime.exec("...") method get Error.
    Does Someone solve it for me.
    Thanks so much.

    I'm having the similar/same issue.
    I'm trying to run SQLLdr from JAVA.
    From a command line, it works fine.. From within JAVA, I get..
    SQL*Loader-704: Internal error: ulconnect: OCIServerAttach [0]
    ORA-12640: Authentication adapter initialization failed.
    Did you find a solution to your problem?

  • Swign application and Runtime.getRuntime().exec(cmd)

    i want to use this program in a swing application, when i press the button it will execute this comand,?? is possible?
    String[] cmd = {"pwd","ls"};
    try {
    Process ls = Runtime.getRuntime().exec(cmd);
    BufferedReader input1 = new BufferedReader(new InputStreamReader(ls.getInputStream()));
    String str = input1.readLine();
    /* Keep looping until we have read all of the output */
    while(str != null) {
    System.out.println(str);
    str = input1.readLine();
    } catch (java.io.IOException el) {
    System.err.println(el);
    import javax.swing.*;
    import java.awt.event.*;
    public class Main{
    JFrame frame;
    public static void main(String[] args){
    Main db = new Main();
    public Main(){
    frame = new JFrame("Show Message Dialog");
    JButton button = new JButton("Click Me");
    button.addActionListener(new MyAction());
    frame.add(button);
    frame.setSize(400, 400);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    public class MyAction implements ActionListener{
    public void actionPerformed(ActionEvent e){
    JOptionPane.showMessageDialog(frame,"hello");
    }

    its pretty hard to read without code tags,
    if your questions is can i use this code in my new swing applications when i press a button
    sure you can you need to make your new program with actionlisterners, your button will then wait until you click on it,
    then you will call your old method when the button is clicked
    I think that what you want sorry if i got the wrong end of the stick

  • Runtime.getRuntime().exec(Command) problems...

    hi, i am trying to run a NET USE command through using:
    Runtime.getRuntime().exec(Command);
    where Command is a valid NET USE command. I know it is valid because i executed this exact command
    from the command line and it worked fine. This class works perfectly under Windows, but then i took it to an
    OS/2 environment and i am getting this error. Does anybody know what i need to do to modify so it will work
    under OS/2? any gotchas? thanks

    i read that article, and there is a lot to go over. I will start that right now. Anyways, i got the actual stack trace
    from this error:
    at java.lang.Win32Process.create(Native Method)
    at java.lang.Win32Process.<init>(Win32Process.java:67)
    at java.lang.Runtime.execInternal(Native Method)
    at java.lang.Runtime.exec(Runtime.java:566)
    at java.lang.Runtime.exec(Runtime.java:428)
    at java.lang.Runtime.exec(Runtime.java:364)
    at java.lang.Runtime.exec(Runtime.java:326)
    at ReconnectReportServer.run(ReportServer.java:499)
    at java.lang.Thread.run(Thread.java:536)
    and, once again, i am running this on OS/2. I compiled it on OS/2 as well. Any ideas? thanks!

  • Strange behaviour of Runtime.getRuntime().exec(command)

    hello guys,
    i wrote a program which executes some commands in commandline (actually tried multiple stuff.)
    what did i try?
    open "cmd.exe" manually (administrator)
    type "echo %PROCESSOR_ARCHITECTURE%" and hit enter, which returns me
    "AMD64"
    type "java -version" and hit enter, which returns me:
    "java version "1.6.0_10-beta"
    Java(TM) SE Runtime Environment (build 1.6.0_10-beta-b25)
    Java HotSpot(TM) 64-Bit Server VM (build 11.0-b12, mixed mode)"
    type "reg query "HKLM\SOFTWARE\7-zip"" returns me:
    HKEY_LOCAL_MACHINE\SOFTWARE\7-zip
    Path REG_SZ C:\Program Files\7-Zip\
    i wrote two functions to execute an command
    1) simply calls exec and reads errin and stdout from the process started:
    public static String execute(String command) {
              String result = "";
              try {
                   // Execute a command
                   Process child = Runtime.getRuntime().exec(command);
                   // Read from an input stream
                   InputStream in = child.getInputStream();
                   int c;
                   while ((c = in.read()) != -1) {
                        result += ((char) c);
                   in.close();
                   in = child.getErrorStream();
                   while ((c = in.read()) != -1) {
                        result += ((char) c);
                   in.close();
              } catch (IOException e) {
              return result;
         }the second function allows me to send multiple commands to the cmd
    public static String exec(String[] commands) {
              String line;
              String result = "";
              OutputStream stdin = null;
              InputStream stderr = null;
              InputStream stdout = null;
              // launch EXE and grab stdin/stdout and stderr
              try {
                   Process process;
                   process = Runtime.getRuntime().exec("cmd.exe");
                   stdin = process.getOutputStream();
                   stderr = process.getErrorStream();
                   stdout = process.getInputStream();
                   // "write" the parms into stdin
                   for (int i = 0; i < commands.length; i++) {
                        line = commands[i] + "\n";
                        stdin.write(line.getBytes());
                        stdin.flush();
                   stdin.close();
                   // clean up if any output in stdout
                   BufferedReader brCleanUp = new BufferedReader(
                             new InputStreamReader(stdout));
                   while ((line = brCleanUp.readLine()) != null) {
                        result += line + "\n";
                   brCleanUp.close();
                   // clean up if any output in stderr
                   brCleanUp = new BufferedReader(new InputStreamReader(stderr));
                   while ((line = brCleanUp.readLine()) != null) {
                        result += "ERR: " + line + "\n";
                   brCleanUp.close();
              } catch (IOException e) {
                   // TODO Auto-generated catch block
                   e.printStackTrace();
              return result;
         }so i try to execute the commands from above (yes, i am using \\ and \" in java)
    (1) "echo %PROCESSOR_ARCHITECTURE%"
    (2) "java -version"
    (3) "reg query "HKLM\SOFTWARE\7-zip""
    the first function returns me (note that ALL results are different from the stuff above!):
    (1) "" <-- empty ?!
    (2) java version "1.6.0_11"
    Java(TM) SE Runtime Environment (build 1.6.0_11-b03)
    Java HotSpot(TM) Client VM (build 11.0-b16, mixed mode, sharing)
    (3) ERROR: The system was unable to find the specified registry key or value.
    the second function returns me:
    (1) x86 <-- huh? i have AMD64
    (2) java version "1.6.0_11"
    Java(TM) SE Runtime Environment (build 1.6.0_11-b03)
    Java HotSpot(TM) Client VM (build 11.0-b16, mixed mode, sharing)
    (3) ERROR: The system was unable to find the specified registry key or value.
    horray! in this version the java version is correct! processor architecture is not empty but totally incorrect and the reg query is still err.
    any help is wellcome
    note: i only put stuff here, which returns me strange behaviour, most things are working correct with my functions (using the Runtime.getRuntime().exec(command); code)
    note2: "reg query "HKLM\HARDWARE\DESCRIPTION\System\CentralProcessor\0" /t REG_SZ" IS working, so why are "some" queries result in ERR, while they are working if typed by hand in cmd.exe?

    ok, i exported a jar file and execute it from cmd:
    java -jar myjar.jar
    now the output is:
    (1) "" if called by version 1, possible to retrieve by version 2 (no clue why!)
    (2) "java version "1.6.0_10-beta"
    Java(TM) SE Runtime Environment (build 1.6.0_10-beta-b25)
    Java HotSpot(TM) 64-Bit Server VM (build 11.0-b12, mixed mode)"
    (3) C:\Program Files\7-Zip\
    so all three problems are gone! (but its a hard way, as i need both functions and parse a lot of text... :/ )
    thanks for the tip, that eclipse changes variables (i really did not knew this one...)

  • Running curl command from a java program using Runtime.getRuntime.exec

    for some reason my curl command does not run when I run it from within my java program and errors out with "https protocol not supported". This same curl command however runs fine from any directory on my red hat linux system.
    To debug the problem, I printed my curl command from the java program before calling Runtime.getRuntime.exec command and then used this o/p to run from the command line and it runs fine.
    I am not using libcurl or anything else, I am running a simple curl command as a command line utility from inside a Java program.
    Any ideas on why this might be happening?

    thanks a lot for your response. The reason why I am using curl is because I need to use certificates and keys to gain access to the internal server. So I use curl "<url> --cert <path to the certificate>" --key "<path to the key>". If you don't mid could you please tell me which version of curl you are using.
    I am using 7.15 in my system.
    Below is the code which errors out.
    public int execCurlCmd(String command)
              String s = null;
              try {
                  // run the Unix "ps -ef" command
                     Process p = Runtime.getRuntime().exec(command);
                     BufferedReader stdInput = new BufferedReader(new
                          InputStreamReader(p.getInputStream()));
                     BufferedReader stdError = new BufferedReader(new
                          InputStreamReader(p.getErrorStream()));
                     // read the output from the command
                     System.out.println("Here is the standard output of the command:\n");
                     while ((s = stdInput.readLine()) != null) {
                         System.out.println(s);
                     // read any errors from the attempted command
                     System.out.println("Here is the standard error of the command (if any):\n");
                     while ((s = stdError.readLine()) != null) {
                         System.out.println(s);
                     return(0);
                 catch (IOException e) {
                     System.out.println("exception happened - here's what I know: ");
                     e.printStackTrace();
                     return(-1);
         }

  • Showing output to a command window

    Hi Friends
    i have an application where i have implemented sockects (server-client) using
    client = new Socket(IP,port num); // client
    server = new ServerSocket(port num); // server
    clientSock = server.accept(); // server
    now im sending a SQL query from client to server and at server side i m executing it through jdbc.
    i have converted my server socket program to windows service(through some software). So there is no command prompt opened for the Server program.
    Now the proiblem is that my requirement is to open a new command window through my server program and show the return result from the query on that window and then close it.
    in my server prog i'have writen
    Process p = Runtime.getRuntime().exec("C:\\WINDOWS\\system32\\cmd.exe");
    but its not getting opened.
    if it does also, i dont have a clue how i'll send the data returned from the query to that window..
    HELP!!!!

    no my project reqirement is that we have to show it
    in command prompt window.
    Now im trying to open this command prompt from my
    server socket program. which is a windows service.
    now the problem is that cmd.exe is getting opened but
    im unable to see it bcoz it is getting opened in my
    server socket service's environment.
    What im looking for is to how to open cmd.exe in new
    environment and send some data to it to display on
    it???Why does it need to be shown in a command window? Is the user going to interact with the program (or the command window). If all you are doing is displaying a result a dialog is a much better answer. Ask your project management about it. If it is only that they like the way the command window looks (which is the only reason if the user does not need to interact with it) then set the back- and fore- ground colors and the font, so that it looks like the command window, and your good.
    If the user does not need to interact with the thing, then your program attempting to interact with a command prompt is only introducing a complication and a requirement that you don't need.
    Like I said, ask your project manager if you can use a dialog. It is not only easier, it is much more flexible (and sensible).

  • Unix command args in Runtime.exec()

    Hi,
    I am doing the following in a method:
    public void shellCommand(String command) {
    Process p = Runtime.getRuntime().exec("ksh -c " + command);
    ....blaa blaa
    }This works fine, but for some reason it only executes the command String up to the first space. So if I pass ls -la to the method it only sees the ls and ignores the rest of the line.
    Has anyone encountered this, or does anyone know how to get round it?
    Thanks

    You could try the exec() variant that takes an array of strings as args.

  • Inconsistent exit code from Runtime.getRuntime().exec

    I'm getting non-deterministic behavior from a call to a native process. Here is the code:
    public class Test {
    public static void main (String[] pArgs) {
    try {
    String cmd[] = { "cmp", "-s",
    pArgs[0],
    pArgs[1] };
    System.err.println("running command: ");
    Process p = Runtime.getRuntime().exec(cmd);
    System.err.println("getting exitcode...");
    int exitcode = p.waitFor();
    System.err.println(exitcode);
    System.err.println(p.exitValue());
    } catch(Exception e) {
    System.err.println("Caught exception while executing cmd");
    e.printStackTrace();
    And here is the output:
    bock@homeruns[~/work/index]10:08> java Test output output.old
    running command:
    getting exitcode...
    0
    0
    bock@homeruns[~/work/index]10:08> java Test output output.old
    running command:
    getting exitcode...
    1
    1
    bock@homeruns[~/work/index]10:08> java Test output output.old
    running command:
    getting exitcode...
    0
    0
    they should all be 1's because the files are different. when i run it on the command line i get:
    bock@homeruns[~/work/index]10:13> cmp -s output output.old ; echo $?
    1
    any help would definitely be appreciated!
    thanks,
    roger

    thanks for the link. i read through it but it doesn't seem to address why the exit code would be inconsistently reported. the other problems described by that link don't seem to apply to this case because i have not experienced hanging and there is no standard input, standard output, or standard error associated with "cmp -s" - all it does is return the appropriate exit code.
    should i be reporting this as a bug to sun? up till recently i've been assuming it was a problem with my code but now i'm not so sure...

  • Runtime.getRuntime().exec problems running with applet

    Hi,
    I am running the command
    Process prc = Runtime.getRuntime().exec("net config workstation");
    InputStream in = prc.getInputStream();
    BufferedReader br = new BufferedReader(new InputStreamReader(in));
    line = br.readLine();
    When running from an application(/stand alone program) on Win-NT it runs fine. But when i include the same code in an Applet and try to run.
    It doesnt return any thing. I see the DOS-prompt open and close and i can also see the required output generated on the DOS prompt, but the program doesnt return any results(null) in the InputStream.
    I even tried to get the ErrorStream incase it is writing some error but it doesnt write anything.
    I tried running some other commands like (cmd.exe /c set) which were able to run on both application as well as from the applet.
    I have tried giving the "net config workstation" command in all flavors
         String[] cmd = new String[3];
         cmd[0] = "cmd.exe";
         cmd[1] = "/C";
         cmd[2] = "net config workstation";     
    And      
         String[] cmd = new String[3];
         cmd[0] = "net.exe";
         cmd[1] = "config";
         cmd[2] = "workstation";          
    AND
         String[] cmd = new String[5];
         cmd[0] = "cmd.exe";
         cmd[1] = "/C";
         cmd[2] = "net.exe";
         cmd[3] = "config";
         cmd[4] = "workstation";     
    AND giing the full path as
    C:\winnt\system32\net.exe config workstation
    But still it doesnt work.
    Can anybody please suggest what is the problem.Its Urgent.....
    Well the purpose of this is to get users system properties like the Logon Domain , server name etc.... or else can anybody suggest a better way to obtain this information.....
    Your reply will be highly appriciated...
    Thanks in Advance......

    Thanks for atleast replying.....
    Sorry, i forgot to mention.... yes OfCourse, i am using an Signed Applet..... That how i was able to run the "set" command.....
    I have made sure it is not sucha simple problem..... the thing is as i have mentioned i know that even "net config workstation" works..... cause i see the output on the DOS prompt,.... but this output is not returned to the Applet in the getInputStream()/getErrorStream().....
    Can anybody please suggest a way to get this output..... OR is there some other way to get the machine's domain, server name etc info from the machine....?????
    Thanks in Advance...!!

  • Returning control from your program to command window

    When I run my program using -
    java testIt take contol of the command window and the prompt doesn't reappear untill I close the program (it has a gui), does anyone know any way I can run my program and keep the prompt on the screen?
    Thanks.

    Windows: javaw test (I think)
    Unix: java test &

  • Does CTAS evaluate "order by" in APEX SQL Command window?

    I need to recreate my table in order to change the column order. When I use the SQL Commands window and run the SQL, it runs successfully, creates the new table, but doesn't reorder the columns.
    Is this expected behavior?

    user648567 wrote:
    Please update your forum profile with a real handle instead of "user648567".
    I need to recreate my table in order to change the column order. When I use the SQL Commands window and run the SQL, it runs successfully, creates the new table, but doesn't reorder the columns.
    What do you mean by "order by"?
    Using an "ORDER BY" clause in a CTAS (in APEX or anywhare else) will have no effect on column order, which is determined by the order in which columns appear in the projection. It will affect the order in which rows are returned by that query, and it will not determine the order in which rows will be returned by querying the new table in the future.
    To change the column order the columns in the SELECT clause of the CTAS would have to be in a different order from those in the original table,

  • I am missing Speakable Items in the Speech commands window

    Hi there,
    I just realized that I can't command any speakable actions, I have NO commands in place in the speech commands window, and my -/user/library/speech/speakable items folder is empty.
    I am administrator and single user of my Mac
    I have no idea why it is empty, i suppose it has been like this from the day I have installed OSX, years ago now.
    I have tried to install OSX from scratch on a separate partition, and of course, with this new system, the commands are in the proper place and the speech commands works.
    However, when I re-import my profile with the SetUp assistant from the original partition, all the commands are lost, since the Speakable items folder is under my personnal home directory, it has just been copied, and left empty by the assistant with all my profile.
    Can anyone help me and tell me which items i should copy from/to where to have the speech commands working ????

    Same here.
    I copied the Speakable Items folder from my old computer, but it still won't work.
    I hope you get an answer!
    ANSWER! - I had the same problem. Open up your Speech and turn off speakable. Than go to commands select address book click on configure if there is any Name that has (NUL) uncheck it. than go and turn on speakable again. Your commands should be there!
    It worked for me! Good Luck!
    Message was edited by: Acumowchek

  • Runtime.getRuntime().exec() and waitFor on Windows

    Hi,
    I'm trying to execute a command on Windows 2000 using Runtime.getRuntime().exec(). After that I make a call to waitFor method of the returned process instance. The wait for call blocks and never releases. I'm trying to do simple things, like a call to "cmd /c dir", using commands that I know that work and do release after that. I need to wait for the command terminating to get the output just after that.
    Thanks in advance,
    Marcio Azevedo.

    It's too bad ignoring the streams couldn't be build into ProcessBuilder. Instead you could do it yourself and extend ProcessBuilder like:
    public class MyProcessBuilder extends ProcessBuilder {
      private boolean ignoreInputStream;
      public  MyProcessBuilder(List<String> command) {
         super(command);
      public  MyProcessBuilder(String... command) {
         super(command);
      public boolean ignoreInputStream() {
        return ignoreInputStream;
      public  ProcessBuilder ignoreInputStream(boolean ignoreInputStream) {
        this.ignoreInputStream= ignoreInputStream;
        return this;
      public Process start() {
        Process process = super.start();
        if (ignoreInputStream()) {
             new InputStreamIgnorer(process.getInputStream());
        return process;
      static class InputStreamIgnorer implements Runnable {
        java.io.InputStream in;
        InputStreamIgnorer(java.io.InputStream in) {
          this.in = in;
          new Thread(this,"InputStreamIgnorer Thread").start();
        public void run() {
          int ch;
          try {
            while((ch = in.read()) != -1) {
                //System.out.print(ch);
          } catch (java.io.IOException ex) {} //ignore
    }Then it could be simpled used like:
    new MyProcessBuilder("cmd","/c", "dir").redirectErrorStream(true).ignoreInputStream(true).start().waitFor();

Maybe you are looking for

  • Bootcamp - Changed partition settings now Windows will not boot

    Hi, I suspect I've created a problem that I've made progressively owrse in trying to solve... Any help appreciated! A month ago I created a bootcamp partition (about 850Gig OS X and 150Gig for Win7), successfully installed Windows 7 and everything wa

  • Flash drive stopped showing up in Finder sidebar

    My flash drive still works perfectly, except for the fact that it stopped showing up on the sidebar under DEVICES. It's a little pesky having to look it up in my desktop when I should be able to find it quickly in every FINDER window.

  • Can't access one tab in iTunes U account

    We have an instructor who is using iTunes U to have students submit files in several different categories. Her site has tabs for each category, and as of yesterday evening, no one can access one of the tabs. Clicking on it produces the "Could not com

  • IPad Mini freezes when adding a new mail account

    Hi, I have just bought the new iPad Mini, and when I try to set up my pop mail account, it comes back with an error saying that the SMTP server doesn't work with SSL activated, and asks if I want to try with this off. If I select yes, it attempts to

  • Error: No more virtual tiles can be allocated

    Hello all When using the following filters in Photoshop CS6 all updated installed: Adaptive Wide Angle Lens Correction I get the following error: No more virtual tiles can be allocated. I have 8 GB of RAM and 100 GB free on my only partition and a Ge