6/15/2006

Set Classpath in Eclipse and NetBeans: a Short Comparison

How to set project classpath in Eclipse and NetBeans are similar: just right-click the project name, choose Properties to bring up the Properties Window. But there are some small variations between the two, mostly of the nature of ease-of-use, or style.

How to set classpath in NetBeans:

In NetBeans Project Properties Window, you click Libraries in the left panel, and in the right panel are 4 categories of classpath you can configure:

  • Compile: Empty by default. Compile-time libraries are automatically propagated to other categories of classpath, so you don't need to repeat the same set of jar files in all 4 categories.

  • Run: By default includes everything in compile-time classpath, and compiled classes (e.g., build/classes).

  • Compile Tests: By default includes everything in compile-time classpath, compiled classes (e.g., build/classes), and JUnit.

  • Run Tests: By default includes classpath for compiling tests, and compiled tests.
The separation of the 4 categories of classpath offers a great deal of flexibility, and in most cases, common-sense default values are used to save configuration efforts. Depending on project types, you may see fewer categories of classpath in NetBeans. For instance, there is no Run classpath for web application project, since we don't directly run a war file.

How to set classpath in Eclipse:

Eclipse manages run classpath and build/compile classpath in different places. To configure run classpath, go to menu Run | Run ... to open up the Run Dialog Window. Your current project should already be selected, otherwise, expand the Java Application node in the left panel and select it. Click Classpath tab in the right panel. The default classpath for the current project is the output folder where all compiled classes reside. You can then click any of these buttons to modify run classpath: Add Projects, Add JARS, Add External JARS, Advanced, etc.

To configure build classpath, in Eclipse Project Properties Window, click Java Build Path in the left panel, and in the right panel choose Libraries tab. There are more classpath-related elements in Eclipse: JARs, External JARS, Variables, Libraries, Class Folders, and other Projects. Click Add External JARs (not Add JARS) if you want to add a jar file to classpath, and click Add Class Folders if you want to add a directory or folder to classpath.

It seems to me Add Variables, Add Libraries, and Add JARS all add collections of classpath elements that are defined inside Eclipse, but I don't know their differences. When I specify classpath in any IDE, I like to explicitly spell out Jar files and directories, without using any IDE-specific artifacts. I suppose the whole purpose of having them is to reuse them in other projects, but I find it's pretty unrealistic due to various jar version differences.

I don't care about the difference between a jar file and a directory, both of them are just an element in the classpath. I like the fact that NetBeans combine them into one single Add JAR/Folder button.

When I create a project in Eclipse 3.2, the default output folder is set to the same folder as source folder, which is not a good idea. At least for me, I don't like mixing class files along with java source files. Wouldn't that also slow down classloading when JVM searches classes, since there are twice as many files to scan. You can change the output folder in project property window | Java Build Path.

11 comments:

Eugene Kuleshov said...

BTW, in Eclipse you can just click on jar or source folder and choose to add it to the build path from the popup menu...

Milind Rao said...

In Eclipse if Project B depends on Project A and Project A needs x.jar and y.jar, I don't need to add them to Project B's classpath while running.

I'm using Netbeans 6.0 and I couldn't get a similar thing going. It's a pain having to add all the dependent projects' jars to the client project.

Secondly, I prefer Eclipse's approach of different buttons for adding folder and jars. If I enter a folder path containing the jars in the File Chooser dialog and hit Enter, it adds the folder to the classpath. In Eclipse, the folder gets opened in the dialog and I can then add the jars in that folder to the classpath.

Anonymous said...

I tried to include the HTTPclient components in my program but always get the error that the package does not exist...

How can I get rid of this??

Shweta said...

I tried to include the HTTPclient components in my program but always get the error that the package does not exist...

How can I get rid of this??

Rajeena said...

I used HttpClient client=new HttpClient();
and added three jar files-
commons-httpclient-3.1.jar,
commons-logging-1.1.1,
commons-pool-1.3
but still showing exception
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
at org.apache.commons.httpclient.HttpClient.(HttpClient.java:66) in the avove line.Can anyone help me?

Angry birds clone said...

Nice post. Great blog. Thanks for sharing. It was very interesting and meaningful.
Fiverr clone script| airbnb clone| Fiverr clone script| Yelp Clone Script|

Logo Design said...

Such a great post share its really very informative post for me.
Keep it up. Banner Design

Kholil(মোঃ খলিলুর রমান) said...

I say many site in blogspot but this is the best site i ever sine. Really, I like your site. Boss, I am hardly interested to learn jave but don't know how to start..

Unknown said...

Kholil,

You can start with Java - Head First book. Learn chapter by chapter and you ll be fine.

List said...

Its not just important to know setting classpath but also How Classpath in Java works, until you know that its difficult to fix issues related to ClassNotFoundException and others.

NANDKISHOR WAGH said...

attractive piece of information, I had come to know about your blog from my friend arjun, ahmedabad,i have read atleast eleven posts of yours by now, and let me tell you, your website gives the best and the most interesting information. This is just the kind of information that i had been looking for, i'm already your rss reader now and i would regularly watch out for the new posts, once again hats off to you! Thanks a lot once again, Regards, Difference Between Classpath and Path