Solved

Using MS-DOS Commands Within Java App to Create Files

Posted on 2013-01-23
9
455 Views
Last Modified: 2013-01-23
I am using the Runtime.getRuntime().exec() method to run MS-DOS commands to, for example, create files.  One example is to execute “dir *abc* /s /b > abc.txt” to create a text file containing the full path names of every file whose filename contains the characters “abc”.

After the successful execution of that command, the Java app does not know that the new file, abc.txt, exists.

Is there something else that must be done to let Java know that this file exists?
0
Comment
Question by:david_m_jacobson
  • 5
  • 3
9 Comments
 
LVL 16

Expert Comment

by:krakatoa
ID: 38811100
You need to make Java see the new file after the exec() returns.

Should be as easy as :

File f = new File("abc.txt");
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 38811265
a. you would need to execute a shell to use stream redirection successfully (i.e. your command must begin with cmd.exe)
b. you don't need to do that to produce a file of paths - Java already has the power to do that without invoking the OS
0
 

Author Comment

by:david_m_jacobson
ID: 38812135
Thank you for that suggestion, but it doesn't work for me.  Here is a snippet of code I am using:

commandLine = "cmd /c dir *\"" + param[0] + "\"* /s /b > \"" + input_filename + "\"";
System.out.println(commandLine);
process = Runtime.getRuntime().exec(commandLine, null, pictures_directoryName_File);
File aFile = new File(input_filename);
if (aFile.exists())
      System.out.println(input_filename + " exists");
else
      System.out.println(input_filename + " does NOT exist");
System.exit(0);

The output looks as follows:

cmd /c dir *"abc"* /s /b > "C:\Users\my_user_name\Desktop\Temporary\abc.txt"
C:\Users\my_user_name\Desktop\Temporary\abc.txt does NOT exist

So clearly, Java doesn't know that 'abc.txt' exists; yet it does exist.

Any other suggestions?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 38812176
If you're determined to do it that way, you need to do it right. See
http://technojeeves.com/joomla/index.php/free/52-runtimeexec
0
 

Author Comment

by:david_m_jacobson
ID: 38812364
Thank you very much.  Reading through the suggested "theory" link brought my attention of the 'waitFor()' method of the Process class, and appears to have solved my problem.

Thank you very much!!
0
 

Author Comment

by:david_m_jacobson
ID: 38812387
I've requested that this question be closed as follows:

Accepted answer: 0 points for david_m_jacobson's comment #a38812364

for the following reason:

I was not rating my own comment here, but rather the Expert's previous comment.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 38812388
I think you might have closed the question in a way you didn't intend ...
0
 

Author Comment

by:david_m_jacobson
ID: 38812723
You are right.  I am about to correct that.  Thank you again.
0
 

Author Closing Comment

by:david_m_jacobson
ID: 38812725
Thank you very much.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question