?
Solved

Using MS-DOS Commands Within Java App to Create Files

Posted on 2013-01-23
9
Medium Priority
?
470 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 86

Accepted Solution

by:
CEHJ earned 2000 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

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!

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month8 days, 18 hours left to enroll

764 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