Results 1 to 13 of 13
Like Tree1Likes
  • 1 Post By eponcedeleon

Thread: Controlling cmd from java...HELP!!

  1. #1
    eponcedeleon is offline Member
    Join Date
    Feb 2010
    Posts
    16
    Rep Power
    0

    Default Controlling cmd from java...HELP!!

    Hi, ive been trying so many things to enter several commands from my java program but i havent been succesful. I hope someone can help...
    This is what i want to do from java which i can do it manually from cmd.
    Ive read this link http://www.javaworld.com/cgi-bin/mai...229-traps.html however, ive been messing around with some of the code but still have had no succes...
    This is basically what i want to do (several times)

    1. open cmd
    2. cd \123\ABC\edf\456
    3. (run this command) dsm.cmd f CREATE b DSKfromjava V bb9server.bbdn.local

    ***dsm.cmd takes this parameters: f CREATE b DSKfromjava V bb9server.bbdn.local

    now i can get in java to step 2 with no problem in jave using something like this..

    Java Code:
    Runtime runTime = Runtime.getRuntime();
    Process pr = runTime.exec("CMD.EXE /c START CMD.EXE /k cd /Bb9-Sicom/blackboard/apps/snapshot/bin");

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,525
    Blog Entries
    7
    Rep Power
    20

    Default

    Instead of messing around with the code in that (good) article, copy and paste it verbatim and try again. Carefully change the code to make it do what you have in mind. Also read and try to understand what that article is trying to tell you.

    kind regards,

    Jos

  3. #3
    eponcedeleon is offline Member
    Join Date
    Feb 2010
    Posts
    16
    Rep Power
    0

    Default

    what do you think I mean by messing around....that means carefully changing the code to what I am trying to do, but havent had success... I thought this forums where to help, not to critice!!

  4. #4
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    Try this:

    Java Code:
    runTime.exec("dsm.cmd –f CREATE –b DSKfromjava –V bb9server.bbdn.local", null, new File("/123/ABC/edf/456"));
    ...replacing the bogus /123... with the proper directory.

    (if you use "\123..., you must instead replace every '\' with '\\', because '\' is the escape character in java)

  5. #5
    eponcedeleon is offline Member
    Join Date
    Feb 2010
    Posts
    16
    Rep Power
    0

    Default

    I appreciate the help, but i am gettin this error...ive tried to change the order because from my understanding cmd should be first.

    Exception in thread "main" java.io.IOException: Cannot run program "dsm.cmd" (in directory "C:\Sicom-Bb9\blackboard\apps\snapshot\bin"): CreateProcess error=2, The system cannot find the file specified
    at java.lang.ProcessBuilder.start(ProcessBuilder.java :459)
    at java.lang.Runtime.exec(Runtime.java:593)
    at java.lang.Runtime.exec(Runtime.java:431)
    at Openexe.main(Openexe.java:20)
    Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
    at java.lang.ProcessImpl.create(Native Method)
    at java.lang.ProcessImpl.<init>(ProcessImpl.java:81)
    at java.lang.ProcessImpl.start(ProcessImpl.java:30)
    at java.lang.ProcessBuilder.start(ProcessBuilder.java :452)
    ... 3 more

  6. #6
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    You're almost there. The problem is that the OS doesn't know where to find the program "dsm.cmd".

    There are 3 ways you can solve that:

    1. Instead of having your command be "dsm.cmd ...", provide a fully qualified path to dsm.cmd. For example, "c:/aaa/111/bbb/dsm.cmd ..."

    2. Edit the PATH environment variable. In windows, you do this by opening control panel, system, advanced. You would then add the directory containing dsm.cmd to that PATH variable.

    3. The second argument to runTime.exec is 'null' in the example I showed before. Instead of passing a null, you can pass define a String[] to tell it where to find dsm.cmd

    Java Code:
    String[] env = new String[] {"path=c:/aaa/111/222"};
    
    runTime.exec("dsm.cmd f CREATE b DSKfromjava V bb9server.bbdn.local", env, new File("/123/ABC/edf/456"));
    The problem with option 3 is that if there are any important environment path dependencies or variables you don't know about, they won't get passed in and you could have other problems.

  7. #7
    eponcedeleon is offline Member
    Join Date
    Feb 2010
    Posts
    16
    Rep Power
    0

    Default

    ok... so if I understood .... add the path of my .cmd to the PATH env variable (that is not a problem)
    My question rises on the code you posted..

    String[] env = new String[] {"path=c:/aaa/111/222"};

    runTime.exec("dsm.cmd –f CREATE –b DSKfromjava –V bb9server.bbdn.local", env, new File("/123/ABC/edf/456"));

    on exec, the first parameter "dsm.cmd –f CREATE –b DSKfromjava –V bb9server.bbdn.local" tell exec to execute .cmd (lucky with the change in path it will find the cmd) then -f CREATE -b .... are the parameters,
    -the 2nd parameter, env, defined with String[] env = new String[] {"path=c:/aaa/111/222"}; should be the same as the PATH environment variable right...
    -3rd parameter...new File("......."); this is the one that i dont know what is for

    hope i explained myself. Thank you for the help!

  8. #8
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    That should work, but you don't need to define PATH *and* set the env String[]. You only need to do *one* of the three actions I listed, not all three. You can if you want to, but it's not necessary.

  9. #9
    eponcedeleon is offline Member
    Join Date
    Feb 2010
    Posts
    16
    Rep Power
    0

    Default

    thanks for the help!!

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,525
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by eponcedeleon View Post
    thanks for the help!!
    All of this is explained in the article you mentioned in your op. You were indeed just messing around instead of reading carefully.

    kind regards,

    Jos

  11. #11
    eponcedeleon is offline Member
    Join Date
    Feb 2010
    Posts
    16
    Rep Power
    0

    Default

    i got the bat to work with all the parameter it takes... now I want to get the input that the bat produces so that i can see it in my console...
    This is what I have...
    Java Code:
        
    Runtime runTime = Runtime.getRuntime();
    Process proc = Runtime.getRuntime().exec("dsm.cmd -f CREATE -V bb9server.bbdn.local -b FROMJAVA5", null, new File("C:\\SicomBb9\\blackboard\\apps\\snapshot\\bin\\"));
    System.out.print(proc.getInputStream()); // simple message handling
    System.out.print(proc.getErrorStream()); // error handing
    proc.waitFor(); // wait until the process is not finished
    Whenever i run this... this is what i get on my java console
    java.io.BufferedInputStream@f62373
    java.io.FileInputStream@19189e1

    but the input i should be getting is something like this...

    Data Source Key Description
    --------------- -----------
    INTERNAL Internal data source used for associating records that are created for use by the Bb system.

    SYSTEM System data source used for associating records that are created via web browser.

    REPLICATED Replicated data source used for root administrator.
    LMS_INTEGRATION LMS Integration data source used for associating records that are created during integration sync operations.

    Test_DSK
    VERANO1 Descripcion del Datasource
    VERANO2 Descripcion del Datasource
    VERANO3
    VERANO4
    FROMJAVA
    FROMJAVA2
    FROMJAVA3
    FROMJAVA4
    FROMJAVA5

  12. #12
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    You have moved from a simple 'launch' application into something that probably requires multiple threads...unless dsm.com just runs, then prints something, and quits.

    The reason java prints out java.io.BufferedInputStream@f62373
    java.io.FileInputStream@19189e1

    ...is because those are the references for the two objects, which is typical for objects that don't have a reasonable string representations.

  13. #13
    eponcedeleon is offline Member
    Join Date
    Feb 2010
    Posts
    16
    Rep Power
    0

    Default

    thank you all for replying... it was fairly simple than i thought..
    Java Code:
    Process proc = Runtime.getRuntime().exec("dsm.cmd -f CREATE -V bb9server.bbdn.local -b "+snapshotFiles[i], null, new File("C:\\Sicom-Bb9\\blackboard\\apps\\snapshot\\bin\\")); 	 
    BufferedReader input = new BufferedReader(new InputStreamReader(proc.getInputStream()));
    //Printing output of dsm to logfile
    while ((line = input.readLine()) != null) {
       log.print(line);
    }
    proc.waitFor(); 			// wait until the process is not finished
    input.close();
    dimogaru likes this.

Similar Threads

  1. Controlling the running os process
    By Muralidharan in forum Threads and Synchronization
    Replies: 1
    Last Post: 03-26-2009, 07:03 AM
  2. Controlling loop using next button
    By cassysumandak in forum New To Java
    Replies: 25
    Last Post: 03-22-2009, 03:04 PM
  3. Controlling loop using next button
    By cassysumandak in forum New To Java
    Replies: 1
    Last Post: 03-21-2009, 02:56 AM
  4. Controlling method calls
    By bugger in forum New To Java
    Replies: 2
    Last Post: 01-04-2008, 01:14 PM
  5. controlling GC
    By ravian in forum Eclipse
    Replies: 2
    Last Post: 01-03-2008, 08:13 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
  •