LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-14-2009, 10:24 PM   #1
jcafaro10
LQ Newbie
 
Registered: Apr 2009
Posts: 25

Rep: Reputation: 15
Running Linux Commands in Java


I'm programming in java on linux trying to have it execute a simple netcat command and print out the results but it refuses to do so. Nothing is printed out so I don't really have any idea if the command is being executed.

Here's my code:
Code:
public static void main(String[] args) throws InterruptedException
	{
		try 
		{
			String result = "";
			String[] commands = new String[]{"/bin/nc.traditional", "www.mywebpage.com", "20000"};
			Process child = Runtime.getRuntime().exec(commands);
			BufferedReader input = new BufferedReader(new InputStreamReader(child.getInputStream()));
		    while ((result = input.readLine()) != null) 
		    {
		    	System.out.println(result);
		    }
		    input.close();
	    } 
		catch (IOException e) 
		{
	    
		}
 
	}
 
Old 04-15-2009, 12:03 AM   #2
1337
Member
 
Registered: Apr 2009
Distribution: Slackware-12.2
Posts: 75

Rep: Reputation: 18
I am going to assume /bin/nc.traditional is the correct location for netcat on your system. Have you tried passing the arguments to exec as one string rather than an array of strings?

I'm also assuming your not using netcat to get a webpage. Netcat is not designed for retrieving webpages!

Did you get the address and port number right? Is it the right protocol (tcp/udp)? Is the server you are connecting to waiting for a command to be issued first? There are several issues involved here which make it hard for me to help you unless I know more. It would be much better if you told me what the project is, (i.e. what are you trying to do, what kind of server are you connecting to, etc.). This way, I'll be able to help you better.

I suggest you try using "nc example.com 20000 -v -v" (use the v twice) as this will make netcat be of very verbose, thereby letting you know what's going on with the communication.
 
Old 04-15-2009, 10:10 AM   #3
jcafaro10
LQ Newbie
 
Registered: Apr 2009
Posts: 25

Original Poster
Rep: Reputation: 15
I've tried it as one string and haven't gotten it to work. It's a fake http server that our professor set up for class. I'm trying to connect on a port thats open, just to see if it lets me. I'll take any output at all, I just want to see something printed in the console.

Code:
try 
{
	// Execute a command with an argument
	String result = "";
	String[] commands = new String[]{"/bin/nc.traditional", "www.mywebserver.com", "20000","-v","-v"};
       //String command = "nc www.mywebserver.com 20000 -v -v";
	Process child = Runtime.getRuntime().exec(commands);
	BufferedReader input = new BufferedReader(new InputStreamReader(child.getInputStream()));
        while ((result = input.readLine()) != null) 
	{
   	     System.out.println(result);
	}	    
	input.close();
}
 
Old 04-15-2009, 12:10 PM   #4
1337
Member
 
Registered: Apr 2009
Distribution: Slackware-12.2
Posts: 75

Rep: Reputation: 18
You need to construct an HTTP-header request and send that to netcat's input (using your input stream) before you will get a response from the server. Google HTTP-header to find the format of a request.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Running linux commands through PHP Nzo Linux - Server 3 06-16-2008 11:16 AM
Running Windows Commands over a Network on Linux jimmy512 Linux - Networking 7 08-08-2007 02:27 PM
running linux commands from C++ mike_morley Programming 3 06-16-2007 11:09 AM
Running Windows commands on linux? itz2000 Linux - Newbie 6 04-19-2006 02:22 PM
Java: passing linux shell commands borgy_t Programming 3 04-19-2005 03:50 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:39 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration