-2

After running mvn package to package compiled java program containing main(),

  • Is it correct that the created jar file doesn't contain dependencies, so when running the jar file, I still have to provide dependencies?

  • Is it correct that the created jar file doesn't indicate which .class file in it contains main() as the entry point, so when running the jar file, I still have to indicate the .class file which contains main()?

Thanks.

Tim
  • 1
  • 141
  • 372
  • 590
  • Just a side comment. Have you tried running a jar file packaged with that Maven command? As a pretty aged SO user you are probably aware of how the site and community works (ie https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users ) – itwasntme Mar 14 '20 at 20:47
  • 1
    Sry, if previous comment might seem to be rude. If all of us would follow the answer from that meta question, there would be only a few new questions on SO in a day. But if would be good if you could avoid multiple questions in single post (and a little bit of pre-investigation also would be fine). – itwasntme Mar 14 '20 at 20:56

1 Answers1

4

It depends. By default, the answer is yes, it does not contain this information.

But Maven can be configured to contain these things. To also include the dependencies, one could use the Maven Assembly plugin and bind it to the package phase with a jar-with-dependencies setting, or use the Maven Shade plugin (also bound to the package phase). They are similar, although I prefer the Shade plugin as it supports so-called transformers that can work around some issues one gets when combining multiple jar files into one.

To have a main-class defined as entry point, you can configure the Maven compile plugin:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.3.1</version>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>my.main.Class</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
    </plugins>
</build>

If configured like that, you don't have to call java -cp my.jar my.main.Class arguments, but can just call it as java -jar my.jar arguments. If you still have additional dependencies, you would call it with java -jar my.jar -cp dependencies/*.jar arguments.

cello
  • 5,356
  • 3
  • 23
  • 28
  • Thanks. When running a jar file, besides using command `java`, can `mvn` be used? If yes, how? Do people often use `mvn` to run a jar file? – Tim Mar 14 '20 at 20:57
  • You can use `mvn exec`, e.g. see here: https://stackoverflow.com/questions/2472376/how-do-i-execute-a-program-using-maven – cello Mar 14 '20 at 20:58
  • I personally prefer to build a jar file and then run it, as it is independent of Maven which (for me) is just a development tool, but not a runtime tool I have on production servers. But I guess opinions vary on that topic. – cello Mar 14 '20 at 21:00
  • Thanks. (1) When using maven to package into a jar file, is the command always `mvn package`, in all the cases (whether indicating the .class file containing main(), and whether containing dependencies)? The differences are only in the .pom files? (2) When using maven to run a jar file, is the command always `mvn exec`? The differences are only in the .pom files? – Tim Mar 14 '20 at 21:01
  • (1) simply said, yes. (Maven is a beast with lots of plugins and options, there are exceptions where the answer is no, but that would be too much here), (2) I have not much experience with it, but I think yes. Typically, one configures everything in the `pom.xml`, and then a simpel `mvn ` does the job. If one needs to build different stuff, one could use so-called profiles in the pom, which can be enabled with `mvn -Pprofile1` for example. – cello Mar 14 '20 at 21:04