Java Project Compilation Errors - java

I have downloaded a complete package of Java product and trying to compile it using Ant. The project compiles with many errors, mostly related to imports starting with "org.apache.commons".
I'm new to Java. It looks to me that some system component is missing.
Some of the errors are:
package org.apache.commons.logging does not exist
package com.ibm.icu.text does not exist
cannot find symbol
What should I do to get rid of those errors?

As Sujee has said you need to include 2 jar files in your classpath. You can find the jars here:
http://download.icu-project.org/files/icu4j/4.4.1.1/icu4j-4_4_1_1.jar
http://apache.forthnet.gr/commons/logging/binaries/commons-logging-1.1.1-bin.zip

org.apache.commons.logging and com.ibm.icu.text are third party Java libraries. Download them from their websites and include in the Java classpath.
Update
Classpath is a list of file system paths which defines the locations of Java classes and libraries. JVM uses this to load the class it needs in the runtime. Usual practice is to put all libraries in a sub folder called 'lib' then add '\lib' in the classpath. My advice is to use a graphical tool like Eclipse so you don't need to manually do this. Please read this wikipedia article for more info about Classpath.

Related

How to find import associated with a class?

Within Eclipse if a class is not found and is available on classpath Eclipse can auto import the required package. What is the mechanism that allows this ?
I doubt there some repository of class files and their associated packages that Eclipse searches...
The reason I tagged this java and scala is I assume the mechanism is same for both languages.
It's as simple as searching through the whole classpath to see if it can match the class name. Then if it can, just adding the path it took to get there. There's nothing mysterious about this
The Eclipse feature simply searches the entire collection of folders in the classpath until it finds one or more matches, then it gives you those matches as options. In pseudocode:
Find Foo.class:
For Each FOLDER in CLASSPATH
For Each PATH...Foo.class Found There
import PATH...Foo.class
You can count JAR files as folders. To see their content, try this on the command-line:
jar tvf MyJar.jar
The class path includes jars containing all JRE classes.

Java trying to find 3rd party class in my package even though that class is in an included JAR

I'm new to Java and NetBeans and I'm having a very hard time getting a simple project started.
I'm trying to include .jar files I need to work on a NetBeans plugin. I can successfully add the .jar files to my project using a variety of attempted methods.
I added .jar files by the project properties and added the 'wrapped jar' files to the project
I added the .jar files to the 'Libraries' item in the Projects explorer tree
Both methods appear to work in the IDE. They allow the desired classes to be accessed in the IDE and no syntax or access errors (etc) are detected. However, when I build and run I get ten pages of errors such as NullPointerException and this doozy:
java.lang.ClassNotFoundException: Will not load class org.netbeans.modules.openide.nodes.NodesRegistrationSupport arbitrarily from one of ModuleCL#2afef4c1[org.openide.nodes] and ModuleCL#2debe24[com.myproject.simplelauncherbutton] starting from SystemClassLoader[316 modules]
com.myproject.simplelauncherbutton is my own package. Why would NetBeans even be looking in my package for this class? And even if it is looking there, how can it be finding the class there, to be confused? I just want to make a NetBeans plugin using a .jar file for support. How can I get this working?!
Solved with help from a coworker. The problem was the way in which I was including packages in my project. I was trying to include packages in my project that did not seem to be available. For example, I needed to use org.openide.nodes, so at the top of my class file I wrote:
import org.openide.nodes;
NetBeans would respond saying it couldn't find this package. When I found a wrapped JAR package containing org.openide.nodes and included that in my project, it generated a slew of errors too long to list here.
However, when I add the module by its English name "Nodes API" in the project properties, everything works fine. I wish there were some documentation or instructions I had been able to find to save me hours of stressing about why I couldn't get NetBeans to recognize the various versions of org.openide.* I was trying to use.

robot framework user java libraries error Test Library "mavenPackage.MyKeyWords.java" does not exist

I am learning RObotoframework using - Java.
With user defined java file I am getting error "Test Library 'mavenPackage.MyKeyWords.java' does not exist".
If I don't use this file, test is running fine.
Only for importing my .Java file, I see this error. Please help!
I installed Jython.
In my "C:\robotfw" I placed...
1. robotframework-2.8.1.jar
2. robotframework-selenium2library-java-1.2.0.13-jar-with-dependencies
3. I placed the whole Java package folder (mavenPackage) here. Inside of this the .java file exists. (mavenPackage.MyKeywords.java)
I set the Classpath for the 1 and 2 JARS.
testcase.txt
* Settings *
Library Selenium2Library
Library mavenPackage.MyKeywords.java
You can't import directly the java file.
You must compile it and put it in your classpath according to the package of the class.
In your classpath you need RF jar, selenium jar and your classes.
You don't even need Jython as it is included in RF jar.

Need help in including all jar files in a directory to the existing project

I have added a piece of sharepoint code to the existing java file which was compiling and working fine. The sharepoint code that is written uses some of the external libraries. Now I need to add the external library to the existing project through ANT.
I have done a few modifications in the build.xml file and hence resolved all the compilation errors. However when the code is getting executed, I get an Error message saying "java.lang.NoClassDefFoundError: net/entropysoft/eci/spi/IContentProviderFactory". Please help me resolving this error.
Also please let me know what needs to be added in the build.xml file to resolve the error.
All the jar files is present in the directory "externallibs"
Thanks,
Rajath
You need to have all the jars in the classpath when running the application:
java -cp externallibs/* com.foo.bar.Main
If it's a Java EE web application, the build process should copy all these jars to the WEB-INF/lib folder of the generated web app structure.
java.lang.NoClassDefFoundError: net/entropysoft/eci/spi/IContentProviderFactory does not mean the class net.entropysoft.eci.spi.IContentProviderFactory is not found. It means the class that is used within this class are not found anywhere in the classpath. This error is thrown when the class loader is trying to load the class but it cannot properly initialize the class definition.
To solve this problem, you will need to look at the source code of the class net.entropysoft.eci.spi.IContentProviderFactory, usually at the import section, and determine what is the missing Java class and which library the missing class is in. Once you know you can add that library to your classpath using the answer by JB Nizet. If you run it from IDE, then you will need to add that library to you build.xml.

finding class name conflicts and jar file version discrepancies and redundancies

Most of the time , the developers will be having hard time to debug the issues related to class loading issues for the reasons like
1 . the class path might have two different jars with the same class having different version.
2. class loading issues.
Although we could use jar utility to delve into each and every jar , it is going to be extremely tedious and error prone.
Is there a tool or some mechanism to resolve this kind of issues .
Though the class loading is not simple in realistic , say how the weblogic will do class loading of a particular ear file.
give a try to tattletale, it works both in ant and maven:
The tool will provide you with reports that can help you
Identify dependencies between JAR files
Find missing classes from the classpath
Spot if a class/package is located in multiple JAR files
Spot if the same JAR file is located in multiple locations
With a list of what each JAR file requires and provides
Verify the SerialVersionUID of a class
Find similar JAR files that have different version numbers
Find JAR files without a version number
Find unused JAR archives
Identify sealed / signed JAR archives
Locate a class in a JAR file
Get the OSGi status of your project
Remove black listed API usage
I find running Java in verbose mode quite handy for resolving class path errors.
It will show you what classes and jars are being loaded by the program.
It can be a quick first step to try fix the problem without using a debugging program.

Categories