java -jar classpath refers jar inside ear - java

I have one big app.ear file which contains all my jar. now i am having another healthcheck.jar which will check the database up or not by connecting to database. now database client is inside the ear file, again i don't want to put the client jars(database client, hibernates jars etc) into healthcheck.jar.
is their a way so that i can exclude packaging database client jar from healthcheck.jar and refer it while run time from inside app.ear i'e
java -jar healthcheck.jar -classpath app.ear/hibernate.jar
Both are stored in common folder.

Instead of bundling in the jar file you could add app1jar.jar as an endorsed jar to the container. It's then available to all installed apps.
https://docs.oracle.com/cd/E17904_01/web.1111/e13706/libraries.htm#WLPRG332

Related

How can I create or delete a file in springboot jar using java program after its deployment

I have folder in class path which contains only resources file's that are to be accessed. I cannot give that files from file system. What I need is after deploying spring boot jar with command
Java -jar somename.jar
I want to create or delete files inside my resource folder in the jar

How do I configure embedded jetty to used war file in executable jar as resource

I have an executable jar with a war file in it. Running the jar extracts the war file and creates a WebAppContext from it using webAppContext.setWar(warFile). Although that works, it seems that webAppContext.setWarResource(warResource) should work. I've tried it creating a resource using new PathResource("file.war") which shows a path like "jar:file:/Users/.../jetty-1.0.0-jar-with-dependencies.jar!/file.war". Sounds promising and conventional, but when I try it I get "java.lang.IllegalArgumentException: not file: scheme". Do I really have to extract the war file or is there a trick?
That would be a nested jar content reference and no Java program can do that.
Option 1: use a live-war (aka an executable-war) instead.
This would be a war file that can be deployed traditionally if you want to, but can also be used standalone from the java command line (and will start it's own server if it needs to).
An example project is maintained by the Eclipse Jetty project at ...
https://github.com/jetty-project/embedded-jetty-live-war
Note: the live-war concept was inspired by work done by the Jenkins project and their live-war.
Option 2: eliminate the WAR file layer entirely in your JAR
Don't package the WAR contents in your JAR as a filename.war, consider using it as an exploded WAR (or war directory) instead.
Just unpack the WAR into your JAR file somewhere safe (like /META-INF/webapps/<app-id>/) and then reference that directory location in your JAR file instead.
Option 3: eliminate the need for the WAR concepts entirely
This is the number one most popular choice.
You deconstruct your WAR file into a ServletContextHandler with configured Servlets and Filters, this also eliminates the need for things like annotation scanning / bytecode scanning (which is quite complicated), you'll also not have to wrangle the nested / isolate classloader (your uber JAR file contains all of the classes and downstream dependencies needed to run your webapp), and this approach will definitely speed up your startup time.

Can't execute war - file: "no main manifest attribute"

i have created a jsp project with webservice in netbeans and i have created a war file to implement it in the AWS but while executing the command "java -jar filename.war it is showing some error
no main manifest attribute, in Hurling_Server.war
root#ip-172-31-21-53:/home/ubuntu# java -jar Hurling_Server.war
no main manifest attribute, in Hurling_Server.war
How you can execute/use the WAR file depends on what it contains and how you have packaged it. WAR files are web archives meant to be deployed in server containers like Tomcat, Jetty, etc.
If this is a regular WAR, deploy it in a server like Tomcat, Jetty and access the web application.
Have you bundled an embedded server like Tomcat or Jetty in the WAR? If yes then most likely it is already executable depending on how you have created the WAR in the first place. If not embedded nay server then, you have to add the MANIFEST.MF under META-INF/. And in the manifest file you have to specify the main class name like:
Main-Class:mypackage1.mypackage2.MyExecutableClass
The WAR structure should look like:
mypackage1/mypackag2/MyExecutableClass
WEB-INF/lib
WEB-INF/classes
META-INF/MANIFEST.MF
With embedded Jetty the MANIFEST and executable main class are placed the following way in WAR:
jetty/bootstrap/JettyBootstrap.class
jetty/bootstrap/LiveWarClassLoader.class
META-INF/MANIFEST.MF
So, why your WAR file is not executing depends on several factors mentioned above. Please check.

Issues launching a JAR file from an executable JAR using Manifest file ClassNotFoundException

I have a project which uses both RMI and JDBC and I need to make it an executable JAR. Now my instructions are to make one JAR file with all my source code which ive included all my .java files. My second JAR needs to be the RMI server .class files and then the third JAR is to have all my RMI client .class files.
Now the first JAR and the third JAR I can make no problem but the issue lies with that in my second I require the use of JDBC so I need to include the mysql-connector-java-5.1.31-bin.jar file in it.
My folder consists of the following layout, only default packages are used and everything is in the root directory.
1st JAR - Source Code Jar File Contains...
A2Interface.java
A2InterfaceImpl.java
A2RmiClient.java
A2RmiServer.java
InvalidLocationException.java
DuplicatedAddressException.java
I used this command to jar it
jar cvf source.jar *.java
2nd JAR - Contains the classes related to RMI server, I also need the mysql JDBC jar file included along with it
And I used this command to JAR it
jar cvfm RMIserver.jar server.txt A2RmiServer.class A2Interface.class
A2InterfaceImpl.class *Exception.class mysql-connector-java-5.1.31-bin.jar
My server.txt Manifest file contains the following
Main-Class: A2RmiServer
Class-Path: mysql-connector-java-5.1.31-bin.jar
-empty line as per the docs-
3rd JAR - Contains the classes related to the RMI client
jar cvfm RMIclient.jar client.txt A2RmiClient.class
A2RmiClient$EventHandler.class
Any my client.txt manifest file contains
Main-Class: A2RmiClient
-empty line as per the docs-
Now everything jars perfect fine and I extract my first JAR file containing all my .java files with no errors. I then attempt to run my RMIserver.jar file with the following command...
First I start the registry..
start rmiregistry 5566
Then I run the executable jar file..
java -cp mysql-connector-java-5.1.31-bin.jar -jar RMIserver.jar
AND here is where I get an Exception of the following
Trouble: java.rmi.ServerException: RemoteException occurred in server thread; nested exception is:
java.rmi.UnmarshalException: error unmarshalling arguments; nested exception is:
java.lang.ClassNotFoundException: A2Interface
I cannot determine why this is doing this at all. If I dont use any jar files my code runs perfectly fine with ZERO exceptions but as soon as I try this I always get exceptions. I have searched many places and some people say that you cant include the mysql JAR file (or any JAR file) like this and even according to the Java doc here http://docs.oracle.com/javase/tutorial/deployment/jar/downman.html
They also mention that its designed for use only for JAR's over the network and not in the same directory, however my professor believes this can be done. I have been trying to figure this out for hours and have come to a wall. I don't know how I can make it work with using the necessary mysql JAR file.
OH and please dont mention using any other tools to create a JAR package as I cannot do that for my assignment or even eclipse. I can only do this from using strictly command line tools.
The ClassNotFoundException is happening in the rmiregistry, and it's being returned to your server application wrapped in a ServerException.
In normal usage, the RMI server annotates the classes with their codebase URL so that other Java processes can load the unknown classes. However, the jar file isn't a usable URL for this purpose (I'm not totally sure, but I think it's because it's generally insecure to load from file: URLs received from the network).
The reason it works when you're not using jar files is because the rmiregistry is able to find the missing classes in its own classpath (because you're running it in the same directory that contains A2Interface.class).
You have two choices:
Serve the class files (or a jar containing them) from an http server running on the RMI server's host, and use the java flag -Djava.rmi.server.codebase=http:... to point to the directory/jar where the classes can be found; or
Insert the common classes in every classpath: server, client, and rmiregistry.
For more detail, see http://docs.oracle.com/javase/7/docs/technotes/guides/rmi/codebase.html.
And here is where I get an Exception of the following
The Registry doesn't have that class available on its CLASSPATH. There are three solutions, in increasing order of difficulty:
Run the Registry inside the server JVM, via LocateRegistry.createRegistry(). When you do this, you must store the result of that call in a static variable, to prevent it being garbage-collected. That also makes it possible to unexport the Registry when you want the JVM to exit.
Run the rmiregistry with an appopriate CLASSPATH. There are several ways to do that, including -J-classpath and setting a CLASSPATH environment variable.
Use the RMI codebase feature.
Now my instructions are to make one JAR file with all my source code which ive included all my .java files. My second JAR needs to be the RMI server .class files and then the third JAR is to have all my RMI client .class files.
Your instructions are incorrect. You need to make four JAR files:
Source code (why?)
.class files that are common to both client and server
.class files that are only used on the server
.class files that are only used on the client.
(3) and (4) need manifests, both to name the Main-Class and to name (2) on the Class-path.
The server deployment needs (2) and (3). The client deployment needs (2) and (4).

No persistence provider WHEN exporting jar file

I have a maven desktop project that uses JPA as persistent layer; this layer deals with one MySQL database and one SQL Server database.
When I run it inside Eclipse, there is no problema; but when I try to export it outside the output when I run jar the console prints the famous:
javax.persistence.PersistenceException: No persistence provider for
EntityManager named axaptaUnitName
axaptaUnitName is the unit that deals with SQL Server. I have tried all three type of exportation from Eclipse, extract required libraries into JAR, package into JAR, and copy in external folder; none of them works.
All libraries (including the connector with SQL Server) are correctly added to classpath; inside Eclipse all works perfect; so I assume that it's some kind of exportation problem.Any suggestions?
Edit: I've tried to replace Microsoft SQL driver with JTDS driver; but the issue still happening.
Here is Work around for this.
I simply exported it as a runnable Jar with option - "Extract required libraries into generated Jar".
Opened generated Jar with a archiever software.
Then I found there is no "Persistence.xml" in META-INF folder.
I dragged my "Persistence.xml" file in that META-INF folder in achiever's window itself.
Closed archiever program.
After that, the PersistenceException was disappeared.
I'm assuming you use the "uber-jar" method where all dependency jars are exploded into one big jar. The problem with this approach is if the jar has files with same relative path they could override each other. Consider this scneario:
// contents of A.jar
com/foo/Class1.class
com/foo/Class2.class
META-INF/persistence.xml
// contents of B.jar
com/bar/Class1.class
com/bar/Class2.class
META-INF/persistence.xml
When A.jar and B.jar are exploded and re-packaged into Uber.jar, the earlier META-INF/persistence.xml will get overwritten causing errors / unwanted behavior
A better solution to deploy your standalone application is to keep all dependencies in their original jar packaging, place them in a single folder and run using command like this (windows):
java -cp "dependency/;myprog.jar" com.foo.MyMainClass
(all dependency jars are placed on the folder "dependency")
I've found one solution:
Instead of exporting project with Eclipse, I have generated a jar with Maven this way.

Categories