Why is parent folder of jar also showing up in referenced libraries? - java

I want to find out why is Eclipse exhibiting this kind of behaviour when I add my own custom runnable jar as an external jar in one of my projects.
It does not do this for any of the other jars I downloaded, such as gson or Spring Boots, but only for the jars I created from my projects.
As you can see in the image below, both gson and my custom jar are in the same place but, when the gson jar was added, the downloads folder didn't show up in "Referenced Libraries". But, as soon as I added my custom jar, it shows up. Why is that so?
(I am adding these external jars via "configure buildpath" option)

I have managed to replicate this behaviour for executable jars. Actually, the problem is not with the executable nature, but with this line from manifest file:
Class-Path: .
Because of the above line, everything within the current folder is added to classpath.
As a solution, remove the line from the generated MANIFEST file within the generated jar file, then add the jar to classpath.

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.

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.

class not found add jar via eclipse

When I was working with java spring hibernate struts2 eclipse project. I used buildpath-> addjar option to add jars from a folder inside the workspace. Even if the class is available it shows class not found exception.
If i put those jar files in web-inf/lib folder everything works.. Should i keep those files in the lib itself? Can i just include those jars in a seperate folder and add jar using eclipse configure build path??
what is the problem ? please help me... thanks in advance...
If you're using a standard Eclipse Dynamic web project, best way to go is to put all your Jars in the WEB-INF\lib folder. This will automatically add them to your build path and also include them in your WAR when you export it.
If you still want to use the Build Path > Add Jar option on the project, and want your Jars to also be included in your WAR, you have to go to the Project Properties > Deployment Assembly configuration and include them there. Of course this is double the work for every Jar.

Eclipse Exporting to jar ignores Manifest class path: directive

I am trying to use the Eclipse export function to create a jar file to be deployed in an Axis2 deployment in Tomcat. When I have source code projects, I can export this jar, rename it to aar, and it works fine. All the classes from all the projects are present inside that exported jar.
Now I convert a few of those projects to jar files (they form a library). The primary project now points to these library jars as external jars in the build path. It builds fine. Now I want to export the same type of jar file from this primary project that no longer has access to the projects containing the library source code.
SOO...I followed these instructions to create my own Manifest.txt file that would point to these external jars using the Class-Path directive. The line appears as follows:
Class-Path: file1.jar ../../libraryJars/file2.jar
So I follow the usual export to jar (not runnable of course; there is NO main here!) with the option to use my Manifest file and the Class-Path directive in that manifest is ignored. I look inside the created jar and the only classes I find are from the primary project. All those classes in the external jars were NOT loaded.
How do I get the classes inside the external jars to be exported with the classes in the primary project when creating this jar? I understood that using the Manifest.txt approach was the way to do this. Perhaps it only works when making a runnable jar (which I cannot do)?
I do NOT want to use something as messy as ANT. If I have to resort to script files to accomplish this task I will just do the copies with a bat file.

Categories