Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Executing commands in Java

807588Apr 14 2009 — edited Apr 15 2009
I'm trying to execute a command (netcat) using java and then print out the output, but nothing seems to be getting printed.

Here's my code:
import java.io.IOException;
import java.io.InputStream;

public class Test 
{
	public static void main(String[] args)
	{
		try 
		{
			// Execute a command with an argument
			//String[] commands = new String[]{"nc", "www.mywebsite.com", "1234"};
	        String command = "nc www.mywebsite.com 1234";
		Process child = Runtime.getRuntime().exec(command);
	        InputStream in = child.getInputStream();
	        int c;
	        while ((c = in.read()) != -1) 
	        {
	            System.out.print((char)c);
	        }
	        in.close();
	    } 
		catch (IOException e) 
		{
	    
		}

	}

}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 13 2009
Added on Apr 14 2009
12 comments
571 views