Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-01-2008, 07:52 AM
Member
 
Join Date: Jul 2008
Posts: 5
nhabibi is on a distinguished road
Problem with Runtime.exec()
Hi all,

I want to get a list of a directory with dir command (windows Vista). I write:

Runtime.getRuntime.exec("cmd.exe /C dir " + MyPath);

but the command can't find the specified path for dir. My question is that how we can pass an argument for command line's programs like dir, copy,etc?

Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-01-2008, 07:56 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,875
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Should be

Code:
getRuntime()
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
(Close on September 4, 2008)

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-01-2008, 08:30 AM
Member
 
Join Date: Jul 2008
Posts: 5
nhabibi is on a distinguished road
Yes, actually it is:
Runtime.getRuntime().exec().
I made a mistake in posting.
What is the problem?
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 07-01-2008, 09:53 AM
Senior Member
 
Join Date: Jun 2008
Posts: 150
masijade is on a distinguished road
Why don't you just use File?

But, did you use the full path? Or did you use a partial path, in which case, your "current path" is probably not what you thought it was.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 07-01-2008, 10:03 AM
Member
 
Join Date: Jul 2008
Posts: 5
nhabibi is on a distinguished road
I ue the full path. I know that I can do it with File, but I want to know how to do with exec().
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 07-01-2008, 10:05 AM
Senior Member
 
Join Date: Jun 2008
Posts: 150
masijade is on a distinguished road
Well, it might help to show the actual code, but probably better is this:

When Runtime.exec() won't - Java World
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 07-01-2008, 10:16 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,875
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Did you get any Exceptions here?
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
(Close on September 4, 2008)

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 07-01-2008, 11:02 AM
Member
 
Join Date: Jul 2008
Posts: 5
nhabibi is on a distinguished road
no exception
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 07-01-2008, 11:22 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,875
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Did you check the output is correct, trying to write into a stream? And also the best way is, use a command string array. Check overloaded functions of exec().
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
(Close on September 4, 2008)

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 07-01-2008, 05:25 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: SW MO, USA
Posts: 707
Norm is on a distinguished road
What happens if you leave off the cmd.exe /C and start the comand with dir?
Have you tried the exec() with other commands and gotten them to work?
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 07-01-2008, 07:06 PM
Member
 
Join Date: Jul 2008
Posts: 5
nhabibi is on a distinguished road
It solved. If a program generates output or error, you should get the stream and write it immediately.

I read the javaworld article, thanks Masijade
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 07-02-2008, 02:35 AM
Member
 
Join Date: Jul 2008
Posts: 12
scotter59 is on a distinguished road
Quote:
Originally Posted by nhabibi View Post
Hi all,

I want to get a list of a directory with dir command (windows Vista). I write:

Runtime.getRuntime.exec("cmd.exe /C dir " + MyPath);

but the command can't find the specified path for dir. My question is that how we can pass an argument for command line's programs like dir, copy,etc?

Might need to have something like this ("cmd.exe /C dir """ + MyPath + """") had a similar issue with vbscript today, damn """" and spaces. This may not be exact but might help with your problem.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Runtime.exec(), handling input and output streams crookshank New To Java 0 06-05-2008 03:41 PM
Ant exec task JavaForums Java Blogs 0 05-20-2008 01:40 PM
Example of RunTime class Java Tip Java Tips 0 01-04-2008 10:32 AM
Exec cmd from Java application javaplus New To Java 0 12-17-2007 09:41 AM
runtime method roo7 Advanced Java 5 11-30-2007 05:35 AM


All times are GMT +3. The time now is 12:25 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org