| | |
put the Dos 's command into java program?
![]() |
•
•
Join Date: Jun 2005
Posts: 6
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: Mar 2004
Posts: 763
Reputation:
Solved Threads: 38
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)
import java.net.*; class NetJunk { public static void main () throws UnknownHostException { String host = "Newton"; InetAddress ia = InetAddress.getByName(host); System.out.println ("IP Address = " + ia.getHostAddress()); } }
•
•
Join Date: Jun 2005
Posts: 6
Reputation:
Solved Threads: 0
•
•
•
•
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)
import java.net.*; class NetJunk { public static void main () throws UnknownHostException { String host = "Newton"; InetAddress ia = InetAddress.getByName(host); System.out.println ("IP Address = " + ia.getHostAddress()); } }
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
Just because I feel like helping:
Compile these two classes, then call
Replace ipconfig with whatever command you want to execute.
Java Syntax (Toggle Plain Text)
//CommandLine.java import java.util.*; import java.io.*; public abstract class CommandLine { public static void exec(String cmd) throws Exception { Runtime rt = Runtime.getRuntime(); Process proc = rt.exec(cmd.toString()); StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(), "ERROR"); StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(), "OUTPUT"); errorGobbler.start(); outputGobbler.start(); } public static void exec(String cmd, OutputStream output) throws Exception { Runtime rt = Runtime.getRuntime(); Process proc = rt.exec(cmd.toString()); StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(), "ERROR", output); StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(), "OUTPUT", output); errorGobbler.start(); outputGobbler.start(); } }
Java Syntax (Toggle Plain Text)
//StreamGobbler.java import java.util.*; import java.io.*; class StreamGobbler extends Thread { InputStream is; String type; OutputStream os; StreamGobbler(InputStream is, String type) { this(is, type, null); } StreamGobbler(InputStream is, String type, OutputStream redirect) { this.is = is; this.type = type; this.os = redirect; } public void run() { try { PrintWriter pw = null; if (os != null) pw = new PrintWriter(os); InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); String line=null; while ( (line = br.readLine()) != null) { if (pw != null) pw.println(line); System.out.println(line); } if (pw != null) pw.flush(); } catch (IOException ioe) { ioe.printStackTrace(); } } }
Java Syntax (Toggle Plain Text)
CommandLine.exec("ipconfig");
![]() |
Similar Threads
- Selection Sort in java (Java)
- Running java command thru java program (Java)
- command prompt +java (Java)
Other Threads in the Java Forum
- Previous Thread: array stack infix and postfix
- Next Thread: using cardlayout in java
| Thread Tools | Search this Thread |
-xlint 911 actionlistener addressbook android api append applet application array arrays automation binary blackberry block bluetooth character class client code compile component consumer csv database desktop developmenthelp eclipse error fractal freeze ftp game gameprogramming givemetehcodez graphics gui html ide image integer j2me j2seprojects japplet java javaarraylist javac javaee javaprojects jni jpanel julia lego linked linux list mac map method mobile netbeans notdisplaying number objects online oriented printf problem program project projects recursion replaydirector reporting researchinmotion rotatetext rsa scanner se server set singleton sms sort sql string swing system test textfields threads time title tree tutorial-sample ubuntu update variablebinding windows working






