简体   繁体   中英

Distributing Java Application and files

I have just written a java application to talk to a blood machine. And it posts its data to a web server using OAuth security and XML, and the application works great.

But now I need to distribute it to customers who have these machines. This program is in a .jar file and its also requires other .jar files which are in my project/dist folder.

During the execution it uses little .png files which are displayed as icons on the screen, that the user can click etc.

I want to some how wrap all of this together (including the images) and distribute them either by an installer or i can use a web server.

What is the best way to accomplish this, i tried to copy the .jar and the dist folder (as per the instructions that came out of netbeans) but when I run java -jar myfile.jar it prints out the text i had from the IOException because i have not included the .png files.

Code for the PNG files: Im using this:

try 
    {
        databaseimage = ImageIO.read(new File("image/database.png"));
        databaseDisconnectedImage = ImageIO.read(new  File("image/database_delete.png"));

    } 
    catch (IOException ioe)
    {
        System.out.println("Problem Creating Systray: "+ioe.getMessage()+" Current Working Directory is: "+System.getProperty("user.dir"));
    }

You have a number of alternatives including the following:

  • Build an installer that installs the relevant files on the user's machine. You can either write it from scratch or use a commercial or open-source tool to create the installer. (Installing on multiple operating system types gets complicated ...)

  • Restructure your application so that all resources (eg images) are fetched via the classpath rather than directly from the file system. Then create an executable "Uber-JAR", by exploding all of the dependent JARs and resources, assembling the files into one tree, and JAR-ing the tree. (The JAR is made executable by adding a couple of entries to the manifest.)

  • Create a JNLP launcher for the application. This only works if the end-user is going to be able to access the website that hosts the application files, but it neatly solves the problems of updating the application, and ensuring that the user is using an up-to-date JVM.

Which is best? Each alternative has pros and cons, and you will need to work out which is best for you .

Heavy-hitting solution is to use Maven and create an executable jar, aka an assembly, aka a jar-with-dependencies. A jar can contain both its dependencies and the meta-data to run it properly.

java -jar Client-1.0-SNAPSHOT-jar-with-dependencies.jar

Would work. I assume there's a similar solution using Ant, which is another tool to build Java programs.

More manually: Just think for a second - whatever command including the classpath you use to run your program, make sure to save that into a script. All files that it needs bundle it up into a .zip and send that over, with the instructions to unzip and cd into it. Ultimately you're just running a bunch of files, you just need to send them over and preserve directory structure so your script will work.

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