Saket
Saket

Reputation: 46137

How to embed rt.jar into my WAR using maven

How can I embed one of the JRE libraries (say, rt.jar) into my WAR using Maven?

Is there a maven plugin/command to do so?

Upvotes: 1

Views: 3839

Answers (3)

Xavi López
Xavi López

Reputation: 27880

As adarshr above has already pointed out, it's not a good idea to provide your own JRE jars.

Just in case you need to include some other jar you don't find on the maven repositories, you can add downloaded jars following these steps:

  1. Put rt.jar in the maven local repository, with for instance

    mvn install:install-file -Dfile=myjar.jar -DgroupId=some.group -DartifactId=someartifact Dversion=1.0 -Dpackaging=jar

  2. Place a dependency to this rt.jar on the pom.xml

    <dependency> <groupId>some.group</groupId> <artifactId>someartifact</artifactId> <version>1.0</version> <scope>runtime</scope> </dependency>

  3. When building the artifact, someartifact.jar will be included in it.

Upvotes: 2

Stephen C
Stephen C

Reputation: 718836

Putting an rt.jar file into your WAR file is not going to cause Java to use it. The web container's classloader should always try the system classloader before looking at the JARs in your webapp, and the system classloader will use the rt.jar on the boot classpath; i.e. the one that is in the JRE installation directory.

This is just as well. If the Java runtime used your rt.jar it could get into an awful mess. Code in your rt.jar could try to make calls to native methods that are not implemented in the Java executable, or it could make calls to internal methods whose behavior and/or signatures have changed in an incompatible way.


If you are planning to do this to "fix" some behavior in the class library, my advice would be:

  1. Don't do it ... figure out a workaround instead.
  2. If you have to do it, then do it by checking out the OpenJDK sources, patching the "bug", building a new JDK / JRE and using that as your JRE.

Upvotes: 6

adarshr
adarshr

Reputation: 62593

I don't think that's a good idea. rt.jar is part of your JRE and is best kept separate.

Upvotes: 4

Related Questions