Results 1 to 13 of 13
- 02-17-2010, 05:01 PM #1
Member
- Join Date
- Feb 2010
- Posts
- 16
- Rep Power
- 0
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");
- 02-17-2010, 05:15 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
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
- 02-17-2010, 05:39 PM #3
Member
- Join Date
- Feb 2010
- Posts
- 16
- Rep Power
- 0
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!!
- 02-18-2010, 05:44 AM #4
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
Try this:
...replacing the bogus /123... with the proper directory.Java Code:runTime.exec("dsm.cmd –f CREATE –b DSKfromjava –V bb9server.bbdn.local", null, new File("/123/ABC/edf/456"));
(if you use "\123..., you must instead replace every '\' with '\\', because '\' is the escape character in java)
- 02-18-2010, 07:45 PM #5
Member
- Join Date
- Feb 2010
- Posts
- 16
- Rep Power
- 0
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
- 02-19-2010, 12:10 AM #6
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
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
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.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"));
- 02-19-2010, 05:37 PM #7
Member
- Join Date
- Feb 2010
- Posts
- 16
- Rep Power
- 0
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!
- 02-20-2010, 12:34 AM #8
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
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.
- 02-22-2010, 06:17 PM #9
Member
- Join Date
- Feb 2010
- Posts
- 16
- Rep Power
- 0
thanks for the help!!
- 02-22-2010, 06:37 PM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
- 02-22-2010, 09:00 PM #11
Member
- Join Date
- Feb 2010
- Posts
- 16
- Rep Power
- 0
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...
Whenever i run this... this is what i get on my java consoleJava 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
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
- 02-23-2010, 04:49 AM #12
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
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.
- 02-23-2010, 06:33 PM #13
Member
- Join Date
- Feb 2010
- Posts
- 16
- Rep Power
- 0
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();
Similar Threads
-
Controlling the running os process
By Muralidharan in forum Threads and SynchronizationReplies: 1Last Post: 03-26-2009, 07:03 AM -
Controlling loop using next button
By cassysumandak in forum New To JavaReplies: 25Last Post: 03-22-2009, 03:04 PM -
Controlling loop using next button
By cassysumandak in forum New To JavaReplies: 1Last Post: 03-21-2009, 02:56 AM -
Controlling method calls
By bugger in forum New To JavaReplies: 2Last Post: 01-04-2008, 01:14 PM -
controlling GC
By ravian in forum EclipseReplies: 2Last Post: 01-03-2008, 08:13 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks