Problem with imports in java? - java

I'm having a problem with imports in one of my java applications.
I've Taken a working JSP out of one Eclipse project, Jar'd up all the classes from that project and put it into a new project.
In the new Project I've imported the jar file generated from the original, and pasted the jsp code into a new JSP document.
I'm getting "The import XX could not be resolved errors".
Anyone one have any idea how to diagnose this, everything seems to be in order.

I have an issue like this once in awhile. Try removing the JAR, rebuilding, and adding the JAR again. This should fix it.
It's an Eclipse bug.
Edit: The above answer is assuming the jar is actually on the build path.

When you say "I've imported the jar file" you mean you have copied it or put it in the classpath?

When you add a Jar file, instead of Import, right click on the project and choose "Add External Library" or "Add External Jar"

It sounds like the jar you imported doesn't appear in your build path. Try right-clicking the project, select Build Path > Add External Archives...
This should put a reference to your jar in the project build path.

Check inside the Jar (just open it from the build path) and see if the paths are correct. If you have foo.Bar as something else than foo/Bar.class that might be it.

Related

unable to find a lib folder in my project

I've been working on some project that involves Jar files to be added to build path. but one of my colleagues said that adding the jars to lib folder is advantageous compared to adding it normally.
Now my problem is, in eclipse, I right click and add a java project, but to my surprise I don't find a lib, earlier when I used to do J2EE apps, I used to find that. please let me know how can I fix this.
Thanks
In eclipse when you create a Java project, it doesn't create the lib folder along with it. This is something you want to do manually. At project level you can right click and add the folder. Then you can paste the jars into the folder, select the jars, right click, build path, Add to Build path.
Alternatively you can directly drag and drop to your Referenced Library. Both the ways are similar in effect, no difference.

Including external jars permanently in NetBeans

I'm building a Java Swing Application. This project requires a jar file called JCalender. I've added this to Libraries folder and everything works fine on my computer in NetBeans. But when i open the same project in my friends NetBeans, it doesn't recognizes this library. I've to manually select the file placed inside the projects dist/lib folder. How to avoid this? please help!
But when i open the same project in my friends NetBeans, it doesn't
recognizes this library. I've to manually select the file placed
inside the projects dist/lib folder. How to avoid this?
You can't avoid this because it's not a problem actually. To compile and run a project you need to have access to the external libraries involved in the development, so if you open your NetBeans project in a different computer than yours you will definitely need to resolve the reference to the external libraries. There's no way for the IDE to do it automatically as far as I know.
Can't i give the relative path to the lib folder for that specific
library somewhere in project properties?
You could just give it a try. IMHO if the real goal is to share a project with other developers then I'd change the strategy. I'd create a Library (Tools -> Libraries) and tell my mates to create the very same library including the JCalendar JAR files in the library's classpath. I'd include this library in the project properties and finally I'd use a versioning tool like Git or SVN to share the project.
By doing this your mates still need a copy of the JAR file wrapped in a NetBeans Library, but the project properties won't point to a fixed/relative path looking for a JAR file but wil include a reference to a given Library. The Library itself will resolve the dependency to the JAR file. If you take a look to the project.properties file you'll see something like this:
javac.classpath=\
${file.reference.jcalendar-1.4.jar}
But if you as I've suggested then you'll see something like this:
javac.classpath=\
${libs.JCalendar.classpath}
Here libs.JCalendar.classpath will resolve the dependency so your mates can have the actual JAR file located in whatever folder they like and the project should compile just fine.
Another option is using Maven to manage the projects dependencies but honestly I'm not a Maven expert so I can't help you in this path.
You need to do a "clean and build" and your jar will be in the dist folder. It will include the external jars
You can read more about it here
I think your problem is due to you are providing absolute path of jar file.
while choosing jar on write side of filechooser there is option of
Relative path and Absolute path there you should click on Absolute path.
i am new to java , but anyway , i have faced the same problem and found a solution for my project
If you are in Netbeans , its would be very easy for you
Let you project name is ABC and all your dependent jar file is under the
the folder MyResourceCollection
now we need to permanently import all jars under this folder ,
So from Netbeans ,
right click on your project name
go to properties
go to library
in the library page , check the "libraries folder" label
you can find a browse button at the right side of the label
click browse and select the MyResourceCollection folder
a new window will come , just press Next-->Next--->Finish
all is done , now check yourself by moving the folder into different location

Java runtime error "NoClassDefFoundError" when referencing external project in Eclipse Android Project

I am struggling with this issue for some time now and I think it is time that I appeal to the SO community.
When creating a new Android Project in Eclipse I am trying to reference an external project [for the time being, it is a simple class with a function that returns a string]. I have tried adding a reference to the project itself, I have tried exporting a .JAR file [adding it to the libs folder] but none of these solutions work, though at compile time the classes are seen, at run time I get the dreaded NoClassDefFoundError.
Because the JAR file is present both in the filesystem and the .classpath file, I believe there might be an issue with the way I exported the JAR.
I am not sure why it doesn't work or what the proper workflow is for adding such [non-Android] modules to a project. Any help would be greatly appreciated!
I have come across this issue in a lot of SO questions but I could not find any solutions that would work
I've actually had the same problem before. There are a couple things you can try.
Make sure that in the project properties for your external project that you have the the "Is Library" checked in the Android tab.
Then export the project as a jar file. (make sure you don't include and files other than the actual java class files. No AndroidManifest.xml etc...)
Include the jar file into your other project in the "libs" folder like you were doing before.
and you should be able to reference the class from the jar now.
If that still doesn't work, you might have an ordering issue, so right click the jar file, and select "Build Path" -> "add to build path", then go to the project properties of your non-library project and select Java Build Path -> Order and Export and move the jar file to the top of the list.
Hopefully this helps!
Cheers

Getting NoClassDefFoundError when using common.lang.StringUtils in android java code?

I am getting this error:-NoClassDefFoundError wherever the code is using StringUtils class or methods .I have correctly added the jar files to the code and is not showing any kind of error in my code(Using Eclipse Indigo).
Is there any kind of point I am missing out. I have used this jar file in other projects but they worked properly.
Please help If any one is getting similar kind of problem?Thanks
Yes, this because of your .jar file didn't import properly. Follow below steps -
Place your .jar file in your project's libs folder .
Import it into your project. And, GoTo project -> properties -> Java build path -> order tab.
Check, whether your .jar file checked and placed in order of 1st. This is the main thing.
Hope these steps helps you. Have a look at below image -
Note that the build classpath is different from the runtime classpath. You must add the required JARs to both.
It's one of the things I don't like in Eclipse...
EDIT: To add JARs to the runtime classpath: in Eclipse, double click the plugin.xml file associated with your project. On the Runtime tab there is a Classpath category. From there, you can add JARs to the runtime classpath.
Here is a screenshot to illustrate this:
Are the jar files in a lib folder or a libs folder?
Newer SDKs require the jar files to be in a libs folder.
I do not know how you execute your code, but it clearly means that the jar is not properly added to your classpath, especially if your code compiles in Eclipse.
Add it to your classpath -or re-check it is well added- when executing and it should work.
Right click on project, Select Build Path -> Configure Build Path and Select Libraries tab. Then with add Jars, add the jar in question.(if you are using eclipse)

Problems with jar

I have a project in Eclipse, that inculdes some files with data(not in classpath), and I want to pack this project to the runnable jar-file, but the problem is, that after I pack it to the jar, it's not located there. How can i add it to the jar?
Create an additional source folder and put your files in there. Unless explicitly told to Eclipse only jars things up in the classpath.
If you cannot do that due to some unmentioned restriction then please revise your question.
I would try making sure it's in the src folder. Eclipse tends to add everything in there into the JAR. If that's not an option, let me know in the comments.
Did you actualised your project's content in Eclipse before making the jar?
Right click on package explorer -> refresh.

Categories