put the Dos 's command into java program?

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2004
Posts: 274
Reputation: mmiikkee12 is an unknown quantity at this point 
Solved Threads: 5
mmiikkee12's Avatar
mmiikkee12 mmiikkee12 is offline Offline
Posting Whiz in Training

Re: put the Dos 's command into java program?

 
0
  #11
Jul 23rd, 2005
It's not Windows-only, you could use System.getProperty("os.name") and a switch block to come up with something equivalent for each OS.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 55
Reputation: Sauce is an unknown quantity at this point 
Solved Threads: 0
Sauce Sauce is offline Offline
Junior Poster in Training

Re: put the Dos 's command into java program?

 
0
  #12
Jul 23rd, 2005
Sorry mmiikkee i wasn't refering to your code, which looked nice btw, but i was refering to the name of the thread "Put the DOS commands into a java program". And yes piggy backing off shell or dos commands are handy in some situations.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 274
Reputation: mmiikkee12 is an unknown quantity at this point 
Solved Threads: 5
mmiikkee12's Avatar
mmiikkee12 mmiikkee12 is offline Offline
Posting Whiz in Training

Re: put the Dos 's command into java program?

 
0
  #13
Jul 23rd, 2005
BTW, I got that code off some site a while back (don't remember which one) when I was writing an app to transfer entire folders from one computer to another, for my own use (most of the details were hardcoded.) I just used xcopy.
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 609
Reputation: freesoft_2000 is an unknown quantity at this point 
Solved Threads: 8
freesoft_2000 freesoft_2000 is offline Offline
Practically a Master Poster

Re: put the Dos 's command into java program?

 
0
  #14
Jul 25th, 2005
Hi everyone,

Check out this thread. It has a very good example by Cheenu

http://www.daniweb.com/techtalkforums/thread27944.html

Richard West
Microsoft uses "One World, One Web, One Program" as a slogan.
Doesn’t that sound like "Ein Volk, Ein Reich, Ein Führer" to you, too?
— Eric S. Raymond

Tell me what type of software do you like and what would you pay for it

http://www.daniweb.com/techtalkforums/thread19660.html
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 1
Reputation: cartmansp is an unknown quantity at this point 
Solved Threads: 0
cartmansp cartmansp is offline Offline
Newbie Poster

Re: put the Dos 's command into java program?

 
0
  #15
Jul 26th, 2007
hello..

could you please tell me how to call the function once uv compiled the 2 java files??im new to java so kindly help me out..

thanx
Last edited by cartmansp; Jul 26th, 2007 at 1:39 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 213
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: put the Dos 's command into java program?

 
0
  #16
Jul 26th, 2007
why has noone told him yet that "the DOS command" will never be available from Java for the simple reason that Java doesn't work on DOS based machines?
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 1
Reputation: alvasheehy is an unknown quantity at this point 
Solved Threads: 0
alvasheehy alvasheehy is offline Offline
Newbie Poster

Re: put the Dos 's command into java program?

 
0
  #17
May 11th, 2009
Hi mmikkee,
I have used your code that you kindly put up and it works brilliantly when I use "ipconfig". However, I want to run a different command with arguments which runs when I navigate to the correct folder. So in the command line I run it as follows.

C:\> cd C:\Users\.....\bin.dbg
C:\Users\.....\bin.dbg>tesseract testImage.tif output

I just don't know how to get this to work using your code. I don't have much experience at all with java or command line to be honest so would greatly appreciate your help as its driving me mad!

Thanks


Originally Posted by mmiikkee12 View Post
Just because I feel like helping:
  1. //CommandLine.java
  2. import java.util.*;
  3. import java.io.*;
  4. public abstract class CommandLine
  5. {
  6. public static void exec(String cmd) throws Exception
  7. {
  8. Runtime rt = Runtime.getRuntime();
  9. Process proc = rt.exec(cmd.toString());
  10. StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(), "ERROR");
  11. StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(), "OUTPUT");
  12. errorGobbler.start();
  13. outputGobbler.start();
  14. }
  15. public static void exec(String cmd, OutputStream output) throws Exception
  16. {
  17. Runtime rt = Runtime.getRuntime();
  18. Process proc = rt.exec(cmd.toString());
  19. StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(), "ERROR", output);
  20. StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(), "OUTPUT", output);
  21. errorGobbler.start();
  22. outputGobbler.start();
  23. }
  24. }
  1. //StreamGobbler.java
  2. import java.util.*;
  3. import java.io.*;
  4. class StreamGobbler extends Thread
  5. {
  6. InputStream is;
  7. String type;
  8. OutputStream os;
  9.  
  10. StreamGobbler(InputStream is, String type)
  11. {
  12. this(is, type, null);
  13. }
  14. StreamGobbler(InputStream is, String type, OutputStream redirect)
  15. {
  16. this.is = is;
  17. this.type = type;
  18. this.os = redirect;
  19. }
  20.  
  21. public void run()
  22. {
  23. try
  24. {
  25. PrintWriter pw = null;
  26. if (os != null)
  27. pw = new PrintWriter(os);
  28.  
  29. InputStreamReader isr = new InputStreamReader(is);
  30. BufferedReader br = new BufferedReader(isr);
  31. String line=null;
  32. while ( (line = br.readLine()) != null)
  33. {
  34. if (pw != null)
  35. pw.println(line);
  36. System.out.println(line);
  37. }
  38. if (pw != null)
  39. pw.flush();
  40. } catch (IOException ioe)
  41. {
  42. ioe.printStackTrace();
  43. }
  44. }
  45. }
Compile these two classes, then call
  1. CommandLine.exec("ipconfig");
Replace ipconfig with whatever command you want to execute.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 3
Reputation: irobot_229 is an unknown quantity at this point 
Solved Threads: 0
irobot_229 irobot_229 is offline Offline
Newbie Poster
 
0
  #18
Oct 15th, 2009
Originally Posted by mmiikkee12 View Post

Replace ipconfig with whatever command you want to execute.

Is that possible to execute all the command available in cmd by a java program?
all that i know is there are .exe files corresponding to commands in windows>system32 folder, those are only executable by Runtime().exec command.what about other commands not having corresponding .exes?

Thanks.
Reply With Quote Quick reply to this message  
Reply

Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC