-->

How are Java applications deployed in the “real wo

2020-05-19 07:58发布

问题:

As a novice to the world of Java programming, this question has always boggled my mind. I first believed that all Java files were compacted into applets and then ran, but I soon realized that this isn't always the case. Could someone explain to me how we actually interweave our Java applications into a real product of everyday life?

TL;DR: How do we implement our code for practical usage?

回答1:

It depends on the application. There are many options depending on how you want your users to use your app. Usually it's packaged as a jar or a specialized jar (war, ear).

In theory, you could zip the raw directory structure with your .class files in it and provide a shell script/instructions that run the java command for the user. I don't recommend this because it's kind of unprofessional and requires you to maintain a shell script for each OS you want to be able to run the program on.

Jar files are used to package libraries but you can also have a manifest file in it that says, "When someone double clicks/executes this, run this class". That class can start up a GUI or be a headless task that responds to the parameters, etc.

You can have applets, like you said. These programs are run in the user's browser.

You can have a war file, which is a way to package a web application. You give this to a web server and it knows how to deploy it so that you can visit the web pages. An example web server/container is tomcat or jetty.

You can have an ear file which can contain other war files inside it. This is used for applications that need other parts of the javaee functionality (ejbs, jms queues, etc.). An example of an application server is jboss or glassfish.

There's also java web start apps. These are apps you can run by visiting a webpage, but they get downloaded to your computer and run on the user's computer (instead of on the server's backend, like in a war/ear).

There's also javafx. I don't know anything about that though. By skimming the FAQ, it appears to be Java's answer to Adobe's Flex. You configure UI components with an xml configuration. I'm not sure what format JavaFX apps use, but it does say, "Deploy on the desktop or in the browser".


As Sotirios Delimanolis mentioned in a comment below, you can build these files with build systems like Ant or Maven. You can also build them "by hand" with the tools that come with the java/javaee sdk. For example, you should have a jar command in your path if you installed the sdk. Here are some details of these build systems:

  • Maven
    1. High level (you tell it what to build, not how to build it)
    2. Much more than just a build system. It also has dependency management, etc.
    3. Opinionated (it uses convention over configuration, each config file generates 1 artifact, etc.)
  • Ant
    1. Low level (you tell it how to build things)
    2. Flexible
    3. Config files can do whatever you want, build as many artifacts as you want
    4. Easy to learn
  • SDK tools
    1. Always up to date. EG: Very rarely, maven/ant may not be able to set a configuration option
    2. Difficult to remember commands
    3. Very low level
    4. By itself, not repeatable (EG: unless you build a script, you will have to type the jar command yourself each time)


回答2:

  • Applets never really caught on and are very rarely used nowadays.
  • Simple applications can be deployed as "executable" JAR files , which are basically ZIP archives with additional metadata that tells the JVM which class contains the main method to run. They can be run on the command line using the -jar option, or in most desktop environments by double-clicking (this requires a JVM to be installed as well).
  • Desktop applications can be deployed via Java Web Start or installers like IzPack or Install4J, but Java desktop applications are not very common either.
  • Most Java software nowadays runs only on servers (web servers or app servers). They are typically deployed as WAR or EAR files, which are also ZIP archives containing classes and other resources. These applications then run inside a server component following the Servlet or EJB standards.


回答3:

Create an executable jar, a war which is dropped into a web server or a library that is used by another project that is one of the previous two.



回答4:

If the application is mean to run on a client, it is packaged as an executable JAR, then further packaged as an Application Bundle (Mac), maybe wrapped in an exe (Windows), or paired with an executable script that will launch the JAR and set any required VM arguments.

If it is part of a web application, then it will be packaged as a WAR or EAR and placed into the appropriate location on the web server.

If it is simply a library, then it is usually packaged as a JAR (non-executable) and distributed as such for integration into larger projects.

applets and then ran, but I soon realized that this isn't always the case

Actually, applets are rare nowadays and their use is discouraged.