Results 1 to 8 of 8
Thread: Command Line
- 03-11-2012, 08:54 PM #1
Senior Member
- Join Date
- Nov 2011
- Posts
- 116
- Rep Power
- 0
Command Line
Hi,
I have a method working fine that runs and executes command line arguments using the Process, Runtime and StreamPumpter classes with certain methods, the code is referred below. The problem is, when i try running the 'dir' command, it doesn't return any directories or files when I test it under main method? Although other commands work fine.
I'm assuming it cant find where its trying to look?
Thanks.
static int execCmd(String cmdLine) throws Exception
{
Process process = Runtime.getRuntime().exec(cmdLine);
StreamPumper outPumper = new StreamPumper(process.getInputStream(), System.out);
StreamPumper errPumper = new StreamPumper(process.getErrorStream(), System.err);
outPumper.start();
errPumper.start();
process.waitFor();
outPumper.join();
errPumper.join();
return process.exitValue();
}
- 03-11-2012, 10:17 PM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: Command Line
Does the method throw any exception? What is it?
- 03-11-2012, 10:24 PM #3
Re: Command Line
Why do they call it rush hour when nothing moves? - Robin Williams
- 03-11-2012, 10:26 PM #4
Senior Member
- Join Date
- Nov 2011
- Posts
- 116
- Rep Power
- 0
Re: Command Line
Yes, they throw IOException and InterruptedException exceptions.
IOException is for the Runtime exec() method.
InterruptedException is for the Process methods.
Would that matter?
- 03-11-2012, 10:44 PM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: Command Line
Yes, exceptions matter because they can be quite informative. The IOException comes with a message that more or less says what the problem is. It's a little cryptic, as such messages tend to be, but ask if you can't understand it.
"c:/Program Files/putty.exe" runs just fine, but "dir" throws an exception and says why.Java Code:import java.io.IOException; public class Foo { public static void main(String[] args) throws IOException { Runtime.getRuntime().exec("c:/Program Files/putty.exe"); Runtime.getRuntime().exec("dir"); } }
- 03-12-2012, 05:45 AM #6
Member
- Join Date
- Mar 2012
- Posts
- 6
- Rep Power
- 0
Re: Command Line
Code below is the example to execute the command in the command prompt.
Moderator edit: link removed.Java Code:import java.io.*; public class doscmd { public static void main(String args[]) { try { Process p=Runtime.getRuntime().exec("cmd /c dir"); p.waitFor(); BufferedReader reader=new BufferedReader(new InputStreamReader(p.getInputStream())); String line=reader.readLine(); while(line!=null) { System.out.println(line); line=reader.readLine(); } } catch(IOException e1) {} catch(InterruptedException e2) {} System.out.println("Done"); } }Last edited by waiheng1986; 03-12-2012 at 10:54 AM.
- 03-12-2012, 09:51 AM #7
Re: Command Line
waheng, this is a forum, not a free advertising site. If you want to promote your blog, we have a section for Reviews/Advertising.
Any question about Java can as well be raised here as on your blog; rest assured that this forum has at least a few thousand times more readers.
If you want to contribute to this site, you're most welcome. In view of this early posting history, any links to your blog are liable to be removed, and repeatedly posting links to the blog may lead to a ban.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 03-12-2012, 10:47 AM #8
Member
- Join Date
- Mar 2012
- Posts
- 6
- Rep Power
- 0
Re: Command Line
Well, sorry for the my foolish action in this forum. Actually i am honestly trying to help on those having problem in java and just promote the blog when i reply.
I promise not to promote my blog in this forum and more and honest in solving others problem.
Sorry and hope you guys can welcome me.
Thank you
Similar Threads
-
Command line argument
By denisatandi in forum New To JavaReplies: 8Last Post: 10-16-2012, 11:37 PM -
Open a URL and read it line by line (Works in Eclipse but not from Command Line)
By rosco544 in forum NetworkingReplies: 16Last Post: 09-17-2011, 02:41 AM -
can i run line by line command in netbean?
By choconlongxu in forum NetBeansReplies: 1Last Post: 07-19-2010, 08:41 PM -
Formatting java command line output - Multi line string
By dricco in forum New To JavaReplies: 2Last Post: 07-02-2010, 02:20 PM -
Unable to execute command line command in java
By LordSM in forum New To JavaReplies: 1Last Post: 08-08-2007, 12:23 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks