The correct way to execute an executable (.exe) file in different situations

this problem is already bugging me, so I was curious how your approaches should handle the following situation:

Imagine that I have two different eclipse projects in the same workspace as ProjectA and ProjectB. ProjectB contains the .exe file in its root, which is called from ProjectA as follows:

Process p = Runtime.getRuntime().exec( "../ProjectB/ProjectB.exe" ); 

The problem is that after deployment, the structure changes a bit: ProjectA is now .jar. Next to this bank is now a folder with ProjectB, which contains ProjectB with its .exe, .jars, and so on. Now the correct call should look like this:

 Process p = Runtime.getRuntime().exec( "ProjectB/ProjectB.exe" ); 

Since I need these structures to stay as they are, and I don't want two different .exec calls in my application for each case, I tried several things to get around this situation:

1) Adding ProjectB as a ClassFolder to ProjectA and allowing ContextClassLoader, where to find ProjectB.exe as follows:

 URL url= Thread.currentThread().getContextClassLoader().getResource( "ProjectB.exe" ); file = new File( url.toURI() ); Process p = Runtime.getRuntime().exec( file.getAbsolutePath() ); 

This solution worked fine until I tried to deploy my application to a network drive.

 Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: URI has an authority component at java.io.File.<init>(Unknown Source) 

2) Since I am not allowed to create a network drive for each client, I tried to work around this problem and did the following:

 URL url= Thread.currentThread().getContextClassLoader().getResource( "ProjectB.exe" ); File file = new File( url.getFile() ); Process p = Runtime.getRuntime().exec( file.getAbsolutePath() ); 

It seemed promising, but unfortunately, until my path was left without a dash ("), as this rating seemed to turn every dash into"% 20 ", which, apparently, does not look like runtimeexecutor Of course, I could replace every "% 20" with "" later, but that seems awkward to me.

Another requirement is that both applications must run in a different JVM, so I call the .exe file using Runtimeexecution. So my last question is: is there a way for ProjectB calls to be clean without any workarounds?

+4
source share
1 answer

A clean way is to define a search path (multiple instances of File that point to another directory). Add the exe name to each path and check if the resulting File exists and uses this path to execute it.

The exception is the security level of Windows: it does not allow you to run unknown EXE files from a network path. See your system administrator for a solution.

Note that you cannot always load exe from the classpath. For example, if they are inside a JAR, this will not work. The Windows launcher does not know how to handle JAR files. The solution here is to unzip the resource into a temporary file and run it from there.

+1
source

All Articles