4

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 as well. I found this script is not executed at all. However, when I move this script outside the jar file, it does execute ! Can anyone tell me why, and give me some solutions to put the script inside the jar while ensure its execution?

Thanks a lot !

user2864740
  • 60,010
  • 15
  • 145
  • 220
Ensom Hodder
  • 1,522
  • 5
  • 18
  • 35

3 Answers3

6

I would do the following:

  • Get InputStream for the file with ClassLoader.getResourceAsStream()
  • Write this InputStream to tmp dir
  • Execute it with Runtime.getRuntime().execute(..)
Konstantin Solomatov
  • 10,252
  • 8
  • 58
  • 88
  • @carlspring yes, i do agree, but it will be better to give a little bit code for step 2. I don't know how to create the temporary file, where the temporary locates, and delete it on exit ? Thanks ~ – Ensom Hodder Jul 05 '12 at 08:51
  • @EnsomHodder To create use something like this: File.createTempFile("prefix", "suffix") – Konstantin Solomatov Jul 05 '12 at 08:52
  • @EnsomHodder To delete use File.deleteOnExit(); – Konstantin Solomatov Jul 05 '12 at 08:52
  • @KonstantinSolomatov so after creating the temp file, I create an outputstream to write the script file into this newly created tmp file, and then call it ? – Ensom Hodder Jul 05 '12 at 09:04
  • @KonstantinSolomatov Excuse me, I am trying your method however i cannot manage to it. Firstly, i get the **inputstream** from the given file name "./src/main/resources/aaa.R" (script to execute) ; secondly, I create a temp file, and from which I create a **FileOutputStream** to write the script in. However it will throws a NullPointerException when my program tries to read bytes array from the inputstream obtained by **getResourcesAsStream**. Could you help me survive from it ? I am very grateful for your help ! – Ensom Hodder Jul 05 '12 at 10:29
  • @EnsomHodder 1. You should make sure that you copied aaa.R to the compiler output dir. 2. You should call getResourceAsStream("/resources/aaa.R") not the full path. – Konstantin Solomatov Jul 05 '12 at 10:34
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/13447/discussion-between-ensom-hodder-and-konstantin-solomatov) – Ensom Hodder Jul 05 '12 at 10:42
1

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

Community
  • 1
  • 1
jmj
  • 237,923
  • 42
  • 401
  • 438
  • You mean extract the whole jar file or only the script file to be executed? And could you please tell me how to extract one single file from jar ? – Ensom Hodder Jul 05 '12 at 07:53
  • Just extract the required file from jar, see the link for more details from answer – jmj Jul 05 '12 at 07:57
  • Thanks for your help. after the execution of the script, I have to remove the extracted files. Seems that it's not very clean ~ but most of all, it works – Ensom Hodder Jul 05 '12 at 08:01
  • By the way. I don't know where the class ***ACWrapper*** comes from? Could you explain it ? thanks – Ensom Hodder Jul 05 '12 at 08:26
1

You will need to extract the script from the jar file:

jar -xvf my.jar com/foo/my.script.sh
carlspring
  • 31,231
  • 29
  • 115
  • 197
  • so you mean it's better to execute this command from the Java program, and then execute the second command (extracted file) ? it seems it's quite simple and doesn't not any external library support ~ – Ensom Hodder Jul 05 '12 at 08:07