Cannot start exe after can assembly

I made a simple utility. In it, I had an exe file to run. I got it to run using:

Runtime.getRuntime().exec(this.getClass().getResource("filename.exe").getPath());

I work fine when I run the program from ide (Netbeans).

But when I try to run exe using the above command after creation (i.e. from the jar created when creating), it does not work at all.

I also tried running this:

Desktop.getDesktop().open(new File("filename.exe"))

but do not use again.

Please, help

+4
source share
1 answer

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-, , :

  • InputStream getResourceAsStream

  • exe- ImputStream

  • temp exe, .


(, 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 {

    /**
     * @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();
        }
    }
}

:

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
+1

All Articles