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..

aasha
  • 446
  • 4
  • 11

2 Answers2

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.

Joey
  • 344,408
  • 85
  • 689
  • 683
  • I have searched so much for any hint but I could not get anything useful. And now within a minute of posting this question I get an answer. Thanks a lot Joey and thanks Stack Overflow. – aasha Jun 06 '12 at 11:48
  • 1
    Adding info: [This will help you to understand how to extract file from JAR to some place on file system](http://stackoverflow.com/questions/4764347/extract-and-load-dll-from-jar) – jmj Jun 06 '12 at 12:08
  • 1
    I think this link is appropriate for extracting jar to any folder. http://stackoverflow.com/questions/1529611/how-to-write-a-java-program-which-can-extract-a-jar-file-and-store-its-data-in-s – aasha Jun 06 '12 at 12:37
  • You don't want to extract the complete JAR, though :) – but yes, adapt for a single file. – Joey Jun 06 '12 at 12:41
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)
deyo.vuk
  • 180
  • 1
  • 3
  • 11
  • As I have mentioned in my question I am getting the path correctly with getResource("mybat.bat"). But when I try to execute the batch script I get this error. – aasha Jun 06 '12 at 11:40