Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Tried with a usecase, found that getResource search for path "file:/path/to/thejar.jar!filename.exe", and is unable to use the exe inside the jar.</p> <p>Tried modifying classpath, but failed.</p> <hr> <p>Refered to spring's strategy, which reads config files in jar with an URL connection, I think the solution can be :</p> <ul> <li><p>get an InputStream with getResourceAsStream</p></li> <li><p>copy the exe form inside the jar to a temp file with the ImputStream</p></li> <li><p>run the temp exe file located outside the jar.</p></li> </ul> <hr> <p>This works (OK when compiled to jar, but NOK in IDE, 'cause "getResource" search at different place):</p> <pre><code>package me.mren.loadresource; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; public class Runner { /** * @param args */ public static void main(String[] args) { try { String filename = "/resources/filename.exe"; System.out.println(Runner.class.getResource(filename)); InputStream fi = Runner.class.getResourceAsStream(filename); File temp = File.createTempFile("temp_exe", ""); System.out.println(temp.getPath()); OutputStream fo = new FileOutputStream(temp); byte[] b = new byte[1024]; int count = 0; while ((count = fi.read(b)) != -1) { fo.write(b, 0, count); } fi.close(); fo.close(); System.out.println(temp.canExecute()); Runtime.getRuntime().exec(temp.getPath()); } catch (Exception e) { e.printStackTrace(); } } } </code></pre> <p>Project's files structure:</p> <pre><code>C:\USERS\REN MIN\DEV ENV\JAVA\WORKSPACE\LOADRESOURCE │ .classpath │ .project │ pom.xml │ ├─.settings │ org.eclipse.jdt.core.prefs │ org.eclipse.m2e.core.prefs │ ├─src │ ├─main │ │ ├─java │ │ │ └─me │ │ │ └─mren │ │ │ └─loadresource │ │ │ Runner.java │ │ │ │ │ └─resources │ │ filename.exe │ │ │ └─test │ ├─java │ └─resources └─target │ loadresource-0.0.1-SNAPSHOT.jar │ ├─classes │ │ filename.exe │ │ │ └─me │ └─mren │ └─loadresource │ Runner.class │ ├─maven-archiver │ pom.properties │ ├─surefire └─test-classes </code></pre> <p>the file structure inside the jar:</p> <pre><code>E:\TEST\RESULT │ .classpath │ .project │ pom.xml │ ├─me │ └─mren │ └─loadresource │ Runner.class │ ├─META-INF │ MANIFEST.MF │ └─resources filename.exe </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload