473,416 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

Flakey Results from Runtime.exec() On Windows 2000

I've been using Runtime.exec() like this:

Runtime rt = Runtime.getRuntime();
try {Process p = rt.exec("MyCommand.bat");} catch (Exception e) {do stuff}

When I start my Java classes, I start them with a batch file that changes to
my apps home directory. I've tried exec() with a full pathname (which
seems to have problems on Windows if it has spaces in it, but I'm not sure
if that's really the problem), with just the simple short batch file name,
and with ".\MyCommand.bat".

I have one class that is VERY simple that has managed to do this okay, but
I've tried this in a number of variations. I even took the simple class
and changed ONLY the batch file name and classname so it would run another
batch file in the same dir. It wouldn't. (Yes, the file had read and exec
permissions set.)

All of these commands worked perfectly on Linux (and I am definately
changing the pathnames to use the correct file separator). So I have:

1) exec(command) that works on Linux, but not on Windows, even with
pathnames corrected,
2) A class that runs a batch file, but will not run others in the same
directory, even if they have the correct permission bits set.

What kind of factors can effect this? I wouldn't think the length of the
filename should matter. I can't figure out what else would be the
difference.

I can post the code of all examples, but it's basically the same as above.
(I've used a String I defined earlier in some cases and also used a string
with quotation marks at the start and end, also.) I just can't figure out
why it works sometimes and not others.

Any ideas?

Thanks!

Hal
Jul 17 '05 #1
1 5052
Additional info at bottom:

Hal Vaughan wrote:
I've been using Runtime.exec() like this:

Runtime rt = Runtime.getRuntime();
try {Process p = rt.exec("MyCommand.bat");} catch (Exception e) {do stuff}

When I start my Java classes, I start them with a batch file that changes
to
my apps home directory. I've tried exec() with a full pathname (which
seems to have problems on Windows if it has spaces in it, but I'm not sure
if that's really the problem), with just the simple short batch file name,
and with ".\MyCommand.bat".

I have one class that is VERY simple that has managed to do this okay, but
I've tried this in a number of variations. I even took the simple class
and changed ONLY the batch file name and classname so it would run another
batch file in the same dir. It wouldn't. (Yes, the file had read and
exec permissions set.)

All of these commands worked perfectly on Linux (and I am definately
changing the pathnames to use the correct file separator). So I have:

1) exec(command) that works on Linux, but not on Windows, even with
pathnames corrected,
2) A class that runs a batch file, but will not run others in the same
directory, even if they have the correct permission bits set.

What kind of factors can effect this? I wouldn't think the length of the
filename should matter. I can't figure out what else would be the
difference.

I can post the code of all examples, but it's basically the same as above.
(I've used a String I defined earlier in some cases and also used a string
with quotation marks at the start and end, also.) I just can't figure out
why it works sometimes and not others.

Any ideas?

Thanks!

Hal


I tried reading the error stream from the process I created -- and got
nothing. So I changed that to the intput stream (which confused me at
first, because I thought I should be reading the output stream). I finally
got it working with the code below. Basically, I have an endless loop in
the thread that reads the output from the process. I added in a 1/100 of a
second delay so it didn't freeze up the system. This leaves a few
questions:

1) How can I tell when this is done and kill the thread? I know I can use
Process.waitFor(), but that locks up, instead of checking if it is done.
(The best I can think of is passing the process to yet another thread that
does nothing but Process.waitFor(); and, after that, Process.destroy();)
2) Is there any way to redirect the output of a Windows program without
sending it to a file so I don't have to read the output to make sure it
runs?

Thanks for any thoughts.

Hal

Thread Runner = new Thread() {
public void run() {
byte[] bOut;
int iLen;
Runtime rt = Runtime.getRuntime();
Process p = null;
String sLine = "MyProgram.bat\n";
try {
p = rt.exec(sLine);
InputStream is = p.getInputStream();
while (true) {
iLen = is.available();
bOut = new byte[iLen];
is.read(bOut);
try {Thread.sleep(10);} catch (Exception e) {}
}
} catch (Exception e)
{System.out.println("Cannot Run Command: " + sLine + ", Error: " + e);}
}
};
Runner.start();
Jul 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Avnish Midha | last post by:
Does the Runtime.exec() method support parameters from non-native locales i.e. does it really support the entire unicode range of characters in the parameters. I am trying ot invoke a C++...
5
by: Ayesha Ahsan | last post by:
Hi, I use Runtime.getRuntime().exec(command) to make my system call. For Windows based Dos, i add "cmd /c" before I type in my system call. So for example make the system call "dir": String...
2
by: James Goldwater | last post by:
I have a situation where I actually _do_ want the command window to show after a runtime.exec() call. (Although we pipe stout/stdin to our own logging mech, the program needs to be able to trap...
1
by: maya2000 | last post by:
Hello, I searched this java newsgroup and tried all suggestions, but still failed. I keep watch a task manager, but cmd.exe never running. All I want to do is run and pop up the cmd.exe using...
6
by: Alan | last post by:
I'm just about to start a project that needs to combine the results of a SQL Server query with the results of an Index Server query. The basic idea is that the user enters/selects a bunch of search...
17
by: Owen Jenkins | last post by:
I have an Access application that is being used by 150+ clients. I develop in 97, convert to 2000 and distribute as a 97 or 2000 mde, or 97 runtime. This limits me to 97 functions. My clients may...
3
by: Gio | last post by:
Hi all, this is my problem: I need to "exec" the following command: C:\Programmi\Soft\soft.exe -v -d C:\dati\$nomefile C:\dati\pdf\$filename and i tried to execute it in php with (one of) the...
2
by: DaBrain | last post by:
I am no DBA, but this is my task. I have an SQL Server 2000 Database that has an "SQL Account" that has execute permission on all Stored procedures. it is what was used by the company. This...
2
by: jerry chapman | last post by:
I am trying to send a command to windows from my java program, and I get an error. The pertinent (?) part of my code follows: public boolean action(Event evt, Object arg) { if...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.