Java: Copying an exe-file and launching afterwards fails
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?
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...
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.
If your path contains forward slashes, you might want to try changing them to backslashes before exec
ing. 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?
Maybe the problem you might me facing is not having the bundled more in the directory. Launch4j may convert your program to exe, but there just be a local directory to the jre. That folder contains the bin and lib files of any existing Java jre. Maybe it helps...
Update Seems like some people are having a tough time understanding what I said. No problem, I'll explain.
Let's take an example that I have created a simple Client Chat App in Java and I exported it into a runnable .../ClientChatApp.jar
file. Yet, it still doesn't have the dependencies within it to run the file and needs the installation of Java in the desktop for doing so. For example here, .../ClientChatApp.jar
still needs the dependencies of javax.swing.*
, java.net.*
and java.io.*
. Hence it will locate those dependencies from the JDK/JRE it is supposed to use.
Now, when it comes to converting from .../ClientChatApp.jar
to .../ClientChatApp.exe
, it will work as expected, only if the desktop has Java installed, so that it can collect the dependencies from there. This is not user friendly since any user who doesn't have Java installed will have to install it first to be able to use your app.
Here's where Launch4j helps. When you are converting from jar to exe using this program, then it helps to redirect where the exe will look for the dependencies in a local folder, usually being .../jre/...
in the sam directory as the .../ClientChatApp.exe
file. Quick tip: To actually do that, go to the same directory as where you want to have the exe file, create a folder there called jre
. Now go to C:/Program Files/Java/<<your JDK or JRE folder>>/
and then select and copy the bin
and lib
folders and copy them. Then go to the directory where you created the folder of jre
and paste both of the folders in the folder.
Many users may have already done it, but why I explained all the above is to give a clear understanding of what the jre
folder does and hence come to the main point, that you will have to also send the jre folder with the exe file to the user, otherwise there is a point of failure while execution.
You can send them into a .zip file or create an installer exe using Inno Setup. I recommend doing more research if you're wanting to use Inno Setup.
That's it. Thank You! (plz upvote me, its my first answer)
精彩评论