Multiple packages in java project and jar files - java

I am trying to build java client for rabbitmq.
I have my source folder as A.mq.client.
Also I have a libs folder where I have my rabbitmq jar files. These jars have been added to build path. Everything works OK in Eclipse.
Then I export the entire project in eclipse as jar. jar contents are .classpath, libs folder and my package folder + rest of stuffs.
Now I am getting exception :
java.lang.ClassNotFoundException: com.rabbitmq.client.Consumer
com.rabbitmq.client.Consumer is in lib jars.
I am running jar using java -cp rabbit.jar A.mq.client.Worker where I want to invoke main method of worker class in my package and Worker is accessing jar packages.

As far as I know, you can't generally package jar files inside of another jar file. Typically, you would have to expand all the various dependent jars prior to packaging up your final jar. If you've ever used Maven, this is what the maven-shade-plugin helps to automate.
See the answer in this question for more information.

The CLASSPATH lists places, where the classes are found - either in a directory or in a jar, but not
directories of directories
directories of jars
jars of directories
jars of jars

Related

Trying to add external Jars to a Java Project (without Maven)

I am trying to add external jars to my Java (without Maven) Project.
But I don't get it to work at all.
The official documentation: https://code.visualstudio.com/docs/java/java-project
says there are 2 ways to do it:
1."You can use the Java Dependency Viewer to add any JAR file to your project."
https://code.visualstudio.com/assets/docs/java/java-project/manage-dependencies.gif
As you can seee in the gif, the dev opens a "referenced libraries" folder.
This folder DOES NOT EXIST for me.
or 2."The other easy way to bring additional JAR files as dependencies is to create a lib/ folder in the root directory of the standalone files and place your JAR files there. Source for foo.jar is automatically detected if there is a foo-sources.jar in the lib/ folder."
https://code.visualstudio.com/docs/java/java-project/lib.mp4
I have the Jar file in the lib\ folder but it still shows me he same error messages.
Are there any steps I am missing?
As it says, place the external jar files in the ...\JAVArpg\lib folder.
So copy them from wherever you have them now. cp ../lib/*.jar lib/ is just an example.
You can use the Windows Explorer. You don't have to copy the files using a command-line.

diffrence between adding jars files in lib folder and setting it in classpath

could you please explain the difference in addng jars in lib folder and adding jars to classpath .
are both just a way to add jars(anyone will work) or is there any difference ?
I tried searching but couldn't find any answer.
If you have a web application project that bundles into a WAR, then adding the jar files into WEB-INF/lib will automatically put all these jar in the classpath of the application when being deployed in a servlet container (Jetty, Tomcat) or in an application server (GlassFish, Wildfly). Note that these libraries will only be available for your single application being deployed, not for any other app deployed in this server.
If you have a simple jar with a lib folder inside it, then adding any jar into this folder will do nothing. For jar files that must be executed on their own, you need to specify the libraries to use in the classpath, otherwise your jar will not run. For this case, it will be better to have the libraries inside a lib filder outside the jar, so you could reference them in the MANIFEST file. On the other hand, you may use maven to generate a fat jar.
If you are using any third party jar files then you need to add it in classpath because you java application tries to find out the class which you are using in you app.
Adding jars in classpath means you are explicitly adding the jar files in classpath at the time of execution.
Adding jars in lib folder will be done in eclipse will internally add the jar files in classpath at the time of execution.
To verify both the things execute the following command
ps -eaf | grep -i java
which will display your app with the dependencies(jar files) in classpath.

How to create Jar file with external folders and Jars

I made a simple standard-lone java Application using Spring,Apache Camel,Activemq for processing messages.
Note: My Application don't have any GUI.
My project structure is in the following way.
SACLib folder have nearly 70 external jars(all Spring,Camel and Activemq corresponding jars).
It's working fine in Eclipse. SO Now We want to deploy into Jar file.I tried in Eclipse,But I didn't seen Rod1,Rod2,Copy1 and SACLib folders in my Jarfile.
after Deploying Jar, If I run FirstConsumer.java it runs Rod1-->ThMapInfratab1-2.exe file. For this I mention Real paths of .exe file.
How can I make Jar file with including all my folders.
Thanks
Well, this is a kind of work that is typically done with build automation tools like Apache Ant, Maven or Gradle, so you can investigate there if you want to make this happen automatically next time.
But, if you want to do it manually...
First, you project needs a META-INF folder where you will place a file called a MANIFEST.
That manifest contains a Main-Class entry pointing to you main class. You can read about this in the Java Tutorial: Setting Application's Entry Point.
But it can also contain a Class-Path entry, pointing to all other jars required by your application and that should be loaded by the executable jar.
You can read about it the Java Tutorial: Adding Classes to your Jar Class Path.
If you are building your executable jar with Eclipse, it will let you choose the MANIFEST file that you want to use during the creation process.
Now, if you want to use build automation tools, there are other answers here that explain how to do it:
Creating a bundle jar with ant
How to create executable jar with dependencies with Maven
How to export an executable jar in Gradle
simply using ant download it , and then make a build.xml file and put it
Here's an simple example of an ant target that will create a jar (named test.jar) that includes all jar files under the lib directory. Maybe this will solve your problem?
for using apache ant, see this
http://ant.apache.org/manual/using.html

Exception in thread "AWT-EventQueue-2" java.lang.NoClassDefFoundError

I get the error above in my Eclipse signed web applet. I would appreciate any help..
It states that my class is not found:
Exception in thread "AWT-EventQueue-2" java.lang.NoClassDefFoundError: com/dermalog/common/exception/DermalogException
Although I have images as proof that it is included in the build:
Image1
Image2
How I added my jar files:
Made a "lib" folder
Copied the jar files into the "lib" folder
clicked "Properties" on the project
Went to build path, libraries
Added the jar reference
Adding a jar to project Java Build Path in an Eclipse project in most cases only means to add it to classpath used for compilation. Items set on build path are also used to create classpath when running your application or applet from within Eclipse.
The build path however does not say anything about the classpath of your applet when it runs in a different context, e.g. on a webpage. There, one needs to make sure that classpath contains all jars needed to run the app.
There are two simple ways to do this:
Add a manifest file to your JAR and specify the classpath (required JARs) there. Then, when deploying / copying your JAR to the location where it is being invoked, copy the dependency JARs as well.
Include all classes from the JARs your applet depends on to your applet's JAR. Eclipse's Export function allows this.
EDIT:
Ok, I checked your jar file and found this:
MANIFEST.MF had no classpath definition. It should contain a line like:
Class-path: dermalog.afis.drawing.compression.jar dermalog.afis.fingercode.jar dermalog.imaging.capturing.jar
The dependency jars should not be packaged in the dependant jar. Instead, they should be just placed in the same directory (or in a subdirectory - but then use that subdirectory in classpath as well).
The page I linked above explains that.

how to package the .class under the web-inf/classes as a jar and put it to the web-inf/lib dir

I have a java web application name webapp,when I export it to a war,the source codes will be compiled to the WEB-INF/classes.
Now I want these classed be compressed to a jar,and put into the WEB-INF/lib.
I have tried this:
create a new java project named webapp_jar.
Copy all the source codes under the webapp/src to the webapp_jar/src,configurate the build path to make the project work.
Add the webapp_jar reference to webapp project.
However ,when I run the webapp,it seems that the classes defined at webapp_jar can not be found.
Is this possible?
BTW,I use the eclipse ee ide.
have you try ant https://ant.apache.org/ ?
you could make a jar and copy it to folder you want and build the war after.
But , if I anderstand, you would package all the app to a jar ?
I think your webapp can't work without a servlet or two ...
This is really easy to do with maven by creating two subject projects, one to build the war, and the other as a jar project and then use the jar project as a dependency on your war project which would put the jar inside the lib directory when packaging the war.
No I use the Web Deployment Assembly settings in eclipse. I worked.
You can find details here.

Categories