954,536 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

put the Dos 's command into java program?

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

v2_vehooi
Newbie Poster
6 posts since Jun 2005
Reputation Points: 10
Solved Threads: 0
 

Maybe this will work:

Process p = Runtime.Runtime().exec("ipconfig");

server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
 

can u write the whole program for me because i just copy this syntax, it cant work. maybe i should add any package?

v2_vehooi
Newbie Poster
6 posts since Jun 2005
Reputation Points: 10
Solved Threads: 0
 

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());
   }
}
Phaelax
Practically a Posting Shark
858 posts since Mar 2004
Reputation Points: 92
Solved Threads: 51
 

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

v2_vehooi
Newbie Poster
6 posts since Jun 2005
Reputation Points: 10
Solved Threads: 0
 

import java.io.*;

server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
 
import java.io.*;


can you explain more detail. pls really help me.
thaks ya

v2_vehooi
Newbie Poster
6 posts since Jun 2005
Reputation Points: 10
Solved Threads: 0
 

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.

cyrus104
Newbie Poster
1 post since Jul 2005
Reputation Points: 10
Solved Threads: 0
 

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.

mmiikkee12
Posting Whiz in Training
274 posts since Oct 2004
Reputation Points: 17
Solved Threads: 5
 

Gotta love people who want help taking an os independent programming language to make a program that is usable in windows only.

Sauce
Junior Poster in Training
55 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 

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.

mmiikkee12
Posting Whiz in Training
274 posts since Oct 2004
Reputation Points: 17
Solved Threads: 5
 

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.

Sauce
Junior Poster in Training
55 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 

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. :D

mmiikkee12
Posting Whiz in Training
274 posts since Oct 2004
Reputation Points: 17
Solved Threads: 5
 

Hi everyone,

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

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

Richard West

freesoft_2000
Practically a Master Poster
623 posts since Jun 2004
Reputation Points: 25
Solved Threads: 10
 

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 :)

cartmansp
Newbie Poster
1 post since Jul 2007
Reputation Points: 10
Solved Threads: 0
 

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?

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 

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

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.

alvasheehy
Newbie Poster
1 post since May 2009
Reputation Points: 10
Solved Threads: 0
 
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.

irobot_229
Newbie Poster
3 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You