Referencing wrong jar file in java build-path - java

In my project we are referencing lot of dependency .jar files.
/lib/xxx.jar
/lib/abc.jar
The xxx.jar file having some (com.search.hit) packages. The same packages are available in abc.jar file.But the problem comes into picture now, where accessing xxx.jar file it doesn't referencing their package(com.search.hit) instead it is referencing abc.jar package.
Could anyone tell how to redirect the flow?

The class from whichever jar comes first on the classpath is the one that is used. The other one might not even exist as far as the classloader is concerned. It's a good idea to avoid conflicts like this.

In Eclipse go to your project build path configuration and click on the Libraries tab.
Then remove the package that you don't want to be accessed from your list and add it again.
This will cause the package to be lower in the priority list and it'll check the other package before the one you just re-added.

This will create problems for you. My suggestion would be to create 2 extra classes for writing getter and setter wrappers for these jar files. Write 2 seperate classes, each one of them will reference just one of them and your project file will use these wrapper classes to invoke functions from these jars. It would be a lot easier that way.

You have to change the package name it can not be same file name with same package in single JVM. JVM will take load randomly one jar class using class loader and ignore the rest.

Related

How to directly make use of JAR file from main resources folder Java

This might be a slightly unusual question but I have a unique situation where something like this is a good solution for me.
I have a jar file (called engine.jar) within my src/main/resources. How can I directly make use of this jar from my main JAVA class? (add this in the build path?) but....
I have another JAR file (of the exact same name i.e. engine.jar) within my Maven Dependencies library which has slightly different code. I want to make use of this jar later on in my main class.
How can I accomplish this in Java?
The name of the jar should not matter by itself. Have you tried renaming 'engine.jar' so there is no conflict and then adding it to the build path?

jar libraries and separate .java files in Eclipse

I am programming in java using Eclipse, I am a novice and I'm stuck with the following problem: I have an Eclipse project that uses an external jar library. In this library there is a specific class that needs to be temporarily modified. I have the source code for this class. Is it possible to somehow include the source file for the code into the project, so that it will "override" the corresponding class in the jar file?
Thank you.
Basically, it's not possible to have two classes with the same signature (package + name) in the classpath but it's possible to import this class in your project in different package and use it instead of the original one.
Another way to solve this problem is to edit the .jar file by removing or changing the class that you need to be different.
However, note that changing an API is almost never a good idea.

JAVA ClassLoad same class name

Yesterday i thought one question ,below is the detail:
I have 3 JAR files, a.jar, b.jar ,c.jar . both these jars files have a class named com.test.Test ,and sayHello() was defined in this class.
I create a web application, i reference a.jar,b.jar,c.jar . And in main method, i involve sayHello(); .at this time, which com.test.Test will be load?
the result is a.jar.
any body tell me the reason ?? thanks in advance!!!
That is what java language specification says. It loads what ever the class first occurs in classpath and ignores other.
Instead of focusing on which one will be loaded, realize that the stuff within the JAR files probably need their com.test.Test class instead of someone else's com.test.Test to work properly. That means for a functional system you'll have to make a way that a.jar finds a.jar's com.test.Test instead of the one in b.jar. The same goes for b.jar finding it's classes in preference to a.jar's.
The only way to do this is to use a framework which adds name spacing beyond the java package mechanism. This is typically done with multiple classloaders, often one for each JAR file. You can write such a thing yourself (Tomcat did), where you need to specify the rules for cross-loader discovery, or use something akin to a OSGi framework.
Whichever Jar File comes first in your classpath will be used..
You can modify your CLASSPATH environment variable to the path of your Jar file
Suppose you modify it as below: -
set CLASSPATH = %CLASSPATH%;.;a.jar;b.jar
then a.jar will be used..
You can also modify it by: -
set CLASSPATH = %CLASSPATH%;.;b.jar;a.jar
In this case, b.jar will be used..
These commands you need to run from your Command Line..
** NOTE: - If you are using any IDE, then they don't use System Classpath.. You need to set different classpath for the IDE you are using..
If you are using an IDE, such as eclipse, you can modify your classpath on the properties of the project, then go to Build Path, and then you have the Order and Export tab where you can move up and down the jars. The one of the top will be the first taken by your application.
This you can also do manually by editing the file called "classpath" which is on your project and move to the top the jar you want your application to use first.

Adding external .jar file in Eclipse

I'm having trouble adding a .jar file I downloaded for my Java project. This is really the first time I've used eclipse, so please bear with me and for some reason (I have no clue why), I just find it somewhat confusing.
I know that in order reference different class files you simply need to create a class library and add it to the build path. From there, all which needs to be done (unless I'm misunderstanding this for whatever reason) is use the "import" keyword to import whatever .jar, .java, or .class/.interface file necessary into the project.
I've tried that with my .jar. I have it referenced in the build path (all I did was just copy the jar to the project directory, and then use the build path option to add it externally), but when ever try to call the object "Delegator", which obviously is a part of the .jar file, it won't read.
Am I missing something here? Seriously, anyone who knows the answer to this - you're relieving a mother of a headache. And before anyone asks - yes, I've searched this one to death. I've found similar questions, but nothing which quite hit what I was looking for. Either that, or I really just lack the common sense.
Right click on project->BuildPath->Libraries->Addexternaljar and then press ok and if it doesnot worked then you should go to the Order and Export tab and checked the jar you have just added in your project. It will solved your problem.
There are several possible reasons, for the question hasn't mentioned the specific failure, and where it has occurred. The following is a list of possible reasons I could think of, but this may not be exhaustive:
You can import a class, in a different package only if the class is public. The only exception is when you are using the class in the same package. If the class is an inner class marked as private, then you're well and truly out of luck. The Delegator class in question might not be public, and that's why you may be unable to use it. This issue ought to be caught by the compiler.
The directory structure within the JAR might not match your package import statements in your classes. This might not be necessary, for Eclipse ought to provide possible fixes, but it is better to verify that nevertheless. Again, the compiler should complain if this is the case.
If the issue is at runtime, then, it is most likely that the JAR is not available in the runtime classpath. You'll need to configure the Runtime configuration, to add the JAR to the runtime classpath. Refer to the Eclipse documentation on run configurations, if you need to know how to change the runtime classpath.
Note:
Exporting the build classpath entries would matter to other projects that depend on the pertinent project; unexported entries will have to be re-imported if required in other projects. This would not apply to a run configuration.
Update
Every Java application needs a main(String[] args] method to start execution. This is the entrypoint for the application. From the comment, it appears that the main method is in a different class. If so, the said class ought to be used to start the application. In Eclipse, a "Run configuration" might be used for the class that lacks this entrypoint, resulting in the described error. One can rectify this by creating a new Run configuration for the class with the said entrypoint. This may be done by one of the following:
editing the existing Run configuration to use the desired Class (the one with the main method). See the above link, in the third bullet point. Edit the value of the class to be launched.
creating a new Run configuration for the desired Class. Usually, you'll need to traverse to the desired class, and run your application (using the Alt+Shift+X+J shortcut) from the said class.
i was facing similar issue with spring jar files but then tried with different jar files and it work so I think , classes defined in jar files were private and not available outside of jar hence you were not able to access the file .
thanks ,
Raju Rathi
Right click on the project--->Build Path--->Configure Build Path...--->In left side you have to choose Java Build Path--->Libraries--->Add External JARs--->ok--->ok
Steps to add jar file in eclipse
1. right click on project
2. click on Bulid Path->configure path
3. click on java Build path
4. Click on libraries tab
5. click on add external jar tab
6. choose jar file
7 click on ok
Copy the .jar file in libs folder which you want to add in your project.
Right click on .jar file -> Add Build Path
Done.

Change the package of a JAR from the default package

I've downloaded a JAR file from my teacher's website containing some classes in the default package, and I'm thus unable to access them from inside a defined package.
I have read that the preferable solution is to repackage the JAR, changing the package name. However I have no idea how to go at it. The solution probably involves using Ant or Jar Jar, but I've no experience with either tool. I would love if someone coould point me in the right direction.
Thanks.
You need to change the sources and recompile then to change the package - simply moving the class files inside the jar (or outside) does not help.
So ask your teacher to give you the sources (or to put the classes in a suitable package), or use a decompiler to do this yourself.
You can unjar/unzip them manually, create the package and jar them back using and IDE or from the command prompt like this. Also, take a look at the ANT documentation on Jar and Unjar which is quite comprehensive.
As #Piyush Instructed use the below command for creating a Jar file.
jar -cvf *.* Example.jar
If you are using eclipse, just unjar the source files into the source folder of a temporary project. Then, create a new project (the real project you will be working on), and under the java/src directory, create the package structure you want. Then it's just a simple matter of drag-n-dropping the source files from the temporary project into the correct packages in the real project. Eclipse will take care of changing the package declaration of each class for you.

Categories