views:

59

answers:

3

Hi,

I want to copy an existing .exe-file from one directory to another and launch it afterwards with Java. Like this:

FileIO.copy( new File( sourceFile ), new File( targetFile ) );
System.out.println( "Existing: " + new File( targetFile ).exists() );
System.out.println( "Launching " + targetFile );
String cmd[] = { targetFile };
Process p = Runtime.getRuntime().exec( cmd );
p.waitFor();
System.out.println( "Result: " + p.exitValue() );

The output is like this:

Existing: true
Launching C:\test\Launcher.new.exe
Result: 2

So Java says that the file is valid and existing, but Windows just can't launch the process because it thinks the file is not there. The pathes are absolute and with backslashes. I also have all permissions on the files so I'm allowed to execute them. The Launcher.new.exe is generated by Launch4j, so it's more or less standalone. At least it doesn't depend on DLLs in the same folder. But strange: It works when I copy and launch the notepad.exe.

One more strange thing: If I don't copy the file by Java but by hand, the launching also fails with the same error.

OS is Vista with SP1.

Any clue?

A: 

Hmm... I wonder if this might be Vista's wonderful User Access Controls at play...

Are you working within Program Files? If so, move everything out into a seperate folder (c:\CopyTest) and try again - see if that helps...

Martin Milan
I'm already testing from the folder "C:\test".I'm suspecting some kind of freaky virtualization too, but I don't know how to check it.
Philip
Any way you can run as Adminstrator to rule out any Vista silliness?
Martin Milan
I'm already running as Administrator.
Philip
A: 

If your path contains forward slashes, you might want to try changing them to backslashes before execing. Also, you should try to make the path absolute, including a drive letter and colon (e.g. C:\test\myprog.exe). Note that if you code the path in a Java String, you need to double up the backslashes...

Once you get that working, you can ease up on those constraints until you figure out what broke your attempt.


EDIT 1: Some common pitfalls with exec() are mentioned in this article. I don't think any of these apply, but you may want to use the coding from the last example to run your .EXE within CMD.EXE to get decent path resolution, error handling and such.


EDIT 2: Your executable file name needs to be interpreted as a long file name. I'm not positive the API can/will handle this. Please try giving the .EXE a short, simple name (just for testing) like NEWPROG.EXE (with no second dot in the name, either!) But first definitely give it a try with CMD.EXE first.


EDIT 3: From reading comments to the other answer: Is it possible your program is indeed running, and itself returning a status of 2 because it is failing to find a file? Is there some way to verify operation of your program, perhaps by calling it from a .CMD batch script that you run from your Java program, and having it write output redirected to a file?

Carl Smotricz
The pathes are absolute and with backslashes. Forgot to mention that. :)
Philip
Added the actual path and filename now.
Philip
Back/forward slashes doesn't matter in Windows.
BalusC
@BalusC: You don't usually make completely incorrect statements. Maybe you meant that back/forward slashes don't matter in Java? That granted, the command is passed as an array of `tokens of a command line` (so says the doc) and therefore probably subject to Windows' syntax constraints. I believe forward slashes would have been a very valid cause for Status 2.
Carl Smotricz
Try yourself in command console. `cd c:/program files` and so on.
BalusC
I did exactly that, of course. In the console in Windows 7, I get a "file not found" if I try to execute a program with a path using forward slashes. I've never encountered a Windows where this wasn't the case, so I'd be interested to hear where you have this working.
Carl Smotricz
A: 

Without more details, it's hard to give specific answer. Check your permissions on the c:\test directory and the permissions on target file you are trying to execute.

mdma
I'd love to add more details, what's missing?Replaced the placeholder with the actual path and filename.
Philip
Does the exe depend on dlls? Are these in the same folder?
mdma
The exe is standalone, more or less. It's generated by Launch4j.
Philip
As a debugging aid, try copying and launching "notepad.exe". This will then tell you if it is a general problem with all exes or a specific problem with your generated exe.
mdma
Nice idea, I tried it. And it works with the notepad.exe. Strange.
Philip
What happens if you open a console and run c:\test\launch.new.exe? It's quite likely that depends upon some dlls, so you need to check if your PATH variable is correct. My guess is that this is a problem loading one of the EXEs dependency dlls.
mdma
This works just fine.
Philip
If you change your command args array to "cmd.exe","/c","c:\test\launch.new.exe". This emulates what the console is doing.
mdma
Nice idea but has the same effect, exitcode == 2.
Philip
Please confirm that you're running your java class as the same user as when you open a console. Also try not copying the file in java, but copy it by hand to your target directory, and try launching that from java. Is FileIO from the apache commons project? Curious if the target file is being closed.
mdma
Same user: Confirmed.FileIO is a utility-class written by us. The files are closed.Copying by hand and not by Java: Just tried and it has the same effect, exitcode == 2. Now things really are getting strange. :/
Philip
Thanks for the test results. One more thing to try - can you launch the source exe instead, see if that works? i.e. change String[] cmd = { sourceFile }
mdma