How to give priority to a jar file in eclipse classpath? - java

There is a class file TagAttributeInfo which is under two of my jar files.
j2ee-6.1.jar,servlet-api-2.3.jar .
Now i need that class to be called from j2ee-6.1.jar but in runtime ,it is taking the class of servlet-api-2.3.jar,which i don't want.
Can anybody help me to prioritize the jar file in class path so that i will give first priority to j2ee-6.1.jar.

You move it up in the classpath order (right click on the project, select Build Path, followed by Configure Build Path and then Order and Export)

Go into your Project Properties > Build Path.
In the Order and Export tab, find j2ee-6.1.jar and press the Up button to move it higher up in the class path order. Keep pressing Up until it is above servlet-api-2.3.jar.
Finally press OK.

The first jar that it finds on the classpath that has the class it's looking for will be used. So you can prioritize your class by putting its jar at the beginning of the classpath.
How exactly you would do this in Eclipse I'm not sure, but there's probably some list of project dependencies that correspond to the jars on the classpath, and hopefully you can order the elements on that list and put your jar on the top. Try looking for it in the project settings panel.

remove servlet-api-2.3.jar, you don't need it.
or move j2ee-6.1.jar higher up than servlet-api-2.3.jar in the lib list.

I don't know if it's part of the spec but placing the "higher" priority jar earlier on the classpath works when I've done this in the past.

Are you sure you need both jar files?
Anyway, in the Build Path dialog you can specify the order for jar files to be searched.

Only one of these jars should be in your classpath, to be able to compile your classes. But at runtime, the jar will be in your application server classpath, and you won't be able to replace it with the other one.
If your container is a JEE6 container, then it uses servlet 3.0 and the servlet-api-2.3.jar is obsolete and should not be in the classpath. If your container only supports servlet api 2.3, then you won't be able to use classes from the jee6 jar in this container.

Related

Which jar file is used during compilation?

Let's assume I have two jar files on classpath when building my project - myJarFile.jar and myJarFileOld.jar. They contain the same packages and the same classes, but the myJarFileOld.jar contains old implementation, which causes that the compilation fails. I'm not asking for solution of this error, I know that I should remove myJarFileOld.jar to make compilation work. However I'd like to know, what mechanism decides which class from which jar file is used during compilation, when both jar files are present?
When a class needs to be loaded, all jar files in the classpath, in order, are scanned. As soon as the class is found, it's loaded.
Not fully sure, but I believe the order of classpath appearance is deciding. If it's found in first jar, then it's not search in another. However I'm pretty sure that class loader will load both jars at the beginning, and you will get some errors about duplicate code. However I'm not sure this, this is probably related to runtime environment.
you have this feature in Eclipse where you can specify the ordering of the jars that you want to be executed from the project classpath.Go to
Project->Select Properties->Select Build Path from left pane-> go to Order and Export Tab->Select Top or Bottom button-> click ok.
The next time you build your project the jar from the classpath will be picked in the order that you have specified.

How to get rid of strange .jar files in "User Entries" of Run Configurations -> ClassPath

I am trying to use log4j to save log files. I have gotten it to work if I create a new Java project and have log4j-1.2.17.jar, slf4j-api-1.7.5.jar and slf4j-log4j12-1.6.4.jar on my build path. Let's call this ProjectA.
I have another project called ProjectB which has these three jar files on buildpath (so far the same as ProjectA), exception this ProjectB *also*has ProjectC on its build path. This doesn't seem to change anything, except when I go into the Run Configurations (Eclipse) of any main method in ProjectB, it'll now have logback-classic-0.9.28.jar on its so-called User Entries of the ClassPath.
This ruins my logging because it says SLF4J: Class path contains multiple SLF4J bindings.
How do I fix this?
Yes, that is true you cannot have more than one slf4j related log files in your class path.
Either you remove that project c from class path
or else
Remove those slf4j jars from class path because indirectly those are added through projectC.
Main thing is there should not be double slf4j related jar files in you class path.
In your launch configuration, remove the classpath entirely, then rebuild it by adding the required projects, without their exported entries, then the other individual jars or class folders that you need. This way, you can configure the classpath for the launch configuration, exactly the way you want it.

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.

How to use a class from JAR in eclipse

I have two jar files - jar1 and jar2. Both of them are located in C:\Eclipse projects\ and I have added the paths to both of them to the Environment Variable CLASSPATH as follows
.;C:\Program Files\Java\jre6\lib\ext\QTJava.zip;C:\Eclipse projects\stdlib.jar;C:\Eclipse projects\algs4.jar
the ".;" at the beginning were there so I left them. Then I added the jars to the project from their location C:\Eclipse projects\ and they showed up as Referenced Libraries. However, when I try to instantiate a class from the jars it does not recognize it. I am also not able to import the jar (import jar1).
After I tried adding a lib folder in the project and I added the jars there. After, I added them as references once again (so not they appear twice in the Referenced Libraries), however, I am still not able to use the inner classes. Any help will be much appreciated.
UPDATE:
Something must be wrong on my end. None of the suggestions worked for me. Here is a video with all the steps: screencast.com/t/gC81YzCsLY0e
RESOLUTION In my project I had a package called TestProject and it seems that those jars needed a default package. After deleting the TestProject package and using a defaultPackage everything worked correctly after adding external JARs as explained below.
I've got the same problem as you today, And no answer from the web can solve it. However, I fixed it at last.
In fact, there is nothing wrong with the setup, it is right to import those jars through "Add External JARs". The real problem is the location/package of you java code. I found that you have to put your .java file in the default package. For example, you will get errors if you put your java code in a package like com.xxx.yyy.ccc, below is an image which shows the right location/package you should use(see WTF.java). After doing that, you program will be able to run.
However, that is how i fixed my problem, i'm not sure that could work for everyone..
In eclipse, right click on a project->Propeties->Java Build Path->Add External JARs (Add JARs if the jar is inside the project's folder) and then choose your jar file.
From now you can use the inner classes of the jars you added. Eclipse will import them when you'll start using them.
Why don't you use these two JARs—— stdlib-package.jar and algs4-package.jar.
And below the code page(http://algs4.cs.princeton.edu/code/)
Q. If I use a named package to structure my code, the compiler can no longer access the libraries in stdlib.jar or algs4.jar. Why not?
A. The libraries in stdlib.jar and algs4.jar are in the "default" package. In Java, you can't access classes in the default package from a named package. If you need to use our libraries with a named package, you can use these package versions: stdlib-package.jar and algs4-package.jar.
Warning: if you are taking Princeton COS 226 or Coursera, Algorithms, Part I or II, you must use the default package verison of our libraries to facilitate grading.
Showing my test success:
If you have a folder with your JAR files into the project:
Right click on the project>Build Path>Configure Build Path;
At the tab "Libraries" click on Add JARs, search and select the JARs files you want to use.
If you have yours JAR files any other place outside the project:
Right click on the project>Build Path>Configure Build Path;
At the tab "Libraries" click on Add External JARs, search and select the JARs files you want to use.

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.

Categories