Solved

Using MS-DOS Commands Within Java App to Create Files

Posted on 2013-01-23
9
458 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying 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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

739 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