RSS Forums RSS
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 9352 | Replies: 15
Reply
Join Date: Jun 2005
Posts: 6
Reputation: v2_vehooi is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
v2_vehooi v2_vehooi is offline Offline
Newbie Poster

put the Dos 's command into java program?

  #1  
Jul 17th, 2005
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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2004
Location: H4x0rville
Posts: 2,105
Reputation: server_crash is on a distinguished road 
Rep Power: 9
Solved Threads: 18
server_crash's Avatar
server_crash server_crash is offline Offline
Postaholic

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

  #2  
Jul 17th, 2005
Maybe this will work:

Process p = Runtime.Runtime().exec("ipconfig");
Reply With Quote  
Join Date: Jun 2005
Posts: 6
Reputation: v2_vehooi is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
v2_vehooi v2_vehooi is offline Offline
Newbie Poster

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

  #3  
Jul 17th, 2005
can u write the whole program for me because i just copy this syntax, it cant work. maybe i should add any package?
Reply With Quote  
Join Date: Mar 2004
Posts: 733
Reputation: Phaelax is on a distinguished road 
Rep Power: 6
Solved Threads: 32
Phaelax Phaelax is offline Offline
Master Poster

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

  #4  
Jul 18th, 2005
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.


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());
   }
}
Reply With Quote  
Join Date: Jun 2005
Posts: 6
Reputation: v2_vehooi is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
v2_vehooi v2_vehooi is offline Offline
Newbie Poster

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

  #5  
Jul 18th, 2005
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.


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());
   }
}

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
Reply With Quote  
Join Date: Jun 2004
Location: H4x0rville
Posts: 2,105
Reputation: server_crash is on a distinguished road 
Rep Power: 9
Solved Threads: 18
server_crash's Avatar
server_crash server_crash is offline Offline
Postaholic

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

  #6  
Jul 18th, 2005
import java.io.*;
Reply With Quote  
Join Date: Oct 2004
Location: On Earth, I think...
Posts: 246
Reputation: mmiikkee12 is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 4
mmiikkee12's Avatar
mmiikkee12 mmiikkee12 is offline Offline
Posting Whiz in Training

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

  #7  
Jul 21st, 2005
Just because I feel like helping:
//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();
 }
}
//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();  
			}
	}
}
Compile these two classes, then call
CommandLine.exec("ipconfig");
Replace ipconfig with whatever command you want to execute.
Reply With Quote  
Join Date: Jul 2005
Posts: 56
Reputation: Sauce is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
Sauce Sauce is offline Offline
Junior Poster in Training

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

  #8  
Jul 22nd, 2005
Gotta love people who want help taking an os independent programming language to make a program that is usable in windows only.
Reply With Quote  
Join Date: Oct 2004
Location: On Earth, I think...
Posts: 246
Reputation: mmiikkee12 is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 4
mmiikkee12's Avatar
mmiikkee12 mmiikkee12 is offline Offline
Posting Whiz in Training

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

  #9  
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  
Join Date: Jul 2005
Posts: 56
Reputation: Sauce is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
Sauce Sauce is offline Offline
Junior Poster in Training

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

  #10  
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  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 12:14 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC