How to execute a script from a JAR file?

I have a maven project in which I am trying to execute a script (written in R). I put this script file in the source code directory. I found that this script is not running at all. However, when I move this script outside the jar file, it runs! Can someone tell me why, and give me some solutions for placing the script inside the jar, and also ensure its execution?

Thanks a lot!

+4
source share
3 answers

I would do the following:

  • Get InputStream for file with ClassLoader.getResourceAsStream ()
  • Enter this InputStream in tmp dir
  • Run it with Runtime.getRuntime (). execute (..)
+6
source

The file inside the jar is no longer a file, so to execute it, you first need to extract it somewhere , and then execute it from the external extracted file path

+1
source

You need to extract the script from the jar file:

jar -xvf my.jar com/foo/my.script.sh 
+1
source

All Articles