I tried using usecase, found that getResource is looking for the path "file: /path/to/thejar.jar! Filename.exe" and cannot use exe inside the jar.
, .
spring, URL-,
, :
(, jar, NOK IDE, " " getResource " ):
package me.mren.loadresource;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
public class Runner {
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();
}
}
}
:
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
:
E:\TEST\RESULT
β .classpath
β .project
β pom.xml
β
ββme
β ββmren
β ββloadresource
β Runner.class
β
ββMETA-INF
β MANIFEST.MF
β
ββresources
filename.exe