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

Maybe this will work:

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

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

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

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

import java.io.*;

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

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.

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.

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

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.

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.

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

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

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?

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.


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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.