简体   繁体   中英

How do you get a jar from your source folder after you compile your program?

I'm running Java in ProcessBuilder, and I'm having trouble launching the process because my file isn't there anymore.

builder = new ProcessBuilder("java", "-jar", getJar());

By "isn't there," I mean that the getJar() method isn't returning the file that it was before I compiled the program.

These two functions worked until I compiled the program into a Jar itself.

public static void setJar(String file) 
{
    try {
        Jar = new File(Server.class.getResource("myFile.jar").toURI()).toString();
    } catch (URISyntaxException e) { printToConsole(Format.ERROR, e.getMessage()); }
}

public static String getJar() { return Jar;}

The project build:

Project:
-> srcFolder
--> mypackage
---> Class
---> File

What you want to do is not directly possible; java applications ship as an abstracted 'resource oracle' concept, and a resource can be anything; from a file, to an entry in a jar, to streamed from a database, an SFTP server, or even generated on the fly.

You then want to run java -jar x , where x must be an actual file.

Your only reliable option is to use the getResource call to obtain a resource, stream this into a temporary jar file, and then execute that. Making temp files and executing them is a bit tricky. Perhaps instead of shipping a java app inside an app and trying to make the OS run the app-in-an-app, maybe just invoke that app directly, and run it all in a single VM.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM