0

I am executing a batch file from java program. When I execute from eclipse it works fine but when I make it a runnable jar it is not able to find the path of the batch file

I kept the batch file as a resource in the source directory. When I print the path of the resource using Class.getResource("/resource/mybat.bat") it gives the path correctly as

  • resources/mybat.bat(jar creation option : package required library into generated jar)
  • file:/C:/Users/aasha.medhi/Desktop/myjar.jar!/resources/mybat.bat(jar creation option : Extract required libraries into generated jar)

If I extract the jar, the resources folder exist.

However when I try to run the jar, it gives "The system cant find the path specified error" for the batch file

I have gone through most of the links and tried out different options but without any luck. I just need a way to create the runnable jar properly.Please help..

4

2 回答 2

1

While Java can read JARs and treat them just as the normal file system, most other applications cannot. In this case cmd can't. You have to extract the batch to a temporary location and run it from there.

于 2012-06-06T11:36:09.973 回答
0

If I understood correctly you have that batch file inside the jar, which should make it available on the classpath (check META-INF manifest file for that).

Once you have the resource available, in your case the batch file, you can reach it via ClassLoader. Inside your class that should trigger the batch file put the following:

this.getClass().getClassLoader().getResource(name)
于 2012-06-06T11:36:15.703 回答