944,081 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 17232
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Jul 17th, 2005
0

put the Dos 's command into java program?

Expand Post »
i would like to put the command Dos into java program. how i using the java to perform command function.
for example, when we type ipconfig this command into command prompt, it will display my pc's ip right. so how i use java program when a user run my program it will perform that function.

another question is when we write the java program right, the java interpreter will created one file called *.class right. but i need to make this can run in each pc even though that pc is no install java. like the exe file can run in anyway just a double click it and the program will run.
thanks for help
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
v2_vehooi is offline Offline
6 posts
since Jun 2005
Jul 17th, 2005
0

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

Maybe this will work:

Process p = Runtime.Runtime().exec("ipconfig");
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004
Jul 17th, 2005
0

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

can u write the whole program for me because i just copy this syntax, it cant work. maybe i should add any package?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
v2_vehooi is offline Offline
6 posts
since Jun 2005
Jul 18th, 2005
-1

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

Here's code for the lazy man. Just change the host name to your computer name, because I'm not doing that for you too. If you use "localhost", it'll just return the local loopback IP.


Java Syntax (Toggle Plain Text)
  1. import java.net.*;
  2.  
  3. class NetJunk
  4. {
  5. public static void main () throws UnknownHostException
  6. {
  7. String host = "Newton";
  8. InetAddress ia = InetAddress.getByName(host);
  9.  
  10. System.out.println ("IP Address = " + ia.getHostAddress());
  11. }
  12. }
Reputation Points: 92
Solved Threads: 51
Practically a Posting Shark
Phaelax is offline Offline
856 posts
since Mar 2004
Jul 18th, 2005
0

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

Quote originally posted by Phaelax ...
Here's code for the lazy man. Just change the host name to your computer name, because I'm not doing that for you too. If you use "localhost", it'll just return the local loopback IP.


Java Syntax (Toggle Plain Text)
  1. import java.net.*;
  2.  
  3. class NetJunk
  4. {
  5. public static void main () throws UnknownHostException
  6. {
  7. String host = "Newton";
  8. InetAddress ia = InetAddress.getByName(host);
  9.  
  10. System.out.println ("IP Address = " + ia.getHostAddress());
  11. }
  12. }
are you say i'm a lazy man!!!
well, fine but this program just solve that particular problem. what i need is i want use all the command's function apply into java program. IP is an example. let say i can use command prompt to shutdown my computer ( or even restart, logoff etc) just type shutdown in the command prompt. so shutdown this word is the one of the Dos command. now i would like to write a java program to control those command. What is the code for java can support this? What is the syntax?
is ok if you have no idea, never mind
anyway thanks you
Reputation Points: 10
Solved Threads: 0
Newbie Poster
v2_vehooi is offline Offline
6 posts
since Jun 2005
Jul 18th, 2005
0

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

import java.io.*;
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004
Jul 19th, 2005
0

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

Quote originally posted by server_crash ...
import java.io.*;
can you explain more detail. pls really help me.
thaks ya
Reputation Points: 10
Solved Threads: 0
Newbie Poster
v2_vehooi is offline Offline
6 posts
since Jun 2005
Jul 19th, 2005
0

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

If you are doing any programing in java then you should know to use your two best friends the API and Google. The api will explain almost everything for you. How do you expect us to help you if you can't help yourself to the simple things.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cyrus104 is offline Offline
1 posts
since Jul 2005
Jul 21st, 2005
0

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

Just because I feel like helping:
Java Syntax (Toggle Plain Text)
  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. }
Java Syntax (Toggle Plain Text)
  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
Java Syntax (Toggle Plain Text)
  1. CommandLine.exec("ipconfig");
Replace ipconfig with whatever command you want to execute.
Reputation Points: 17
Solved Threads: 5
Posting Whiz in Training
mmiikkee12 is offline Offline
274 posts
since Oct 2004
Jul 22nd, 2005
0

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

Gotta love people who want help taking an os independent programming language to make a program that is usable in windows only.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Sauce is offline Offline
55 posts
since Jul 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: array stack infix and postfix
Next Thread in Java Forum Timeline: using cardlayout in java





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC