Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Please Help wit java packages and Project Builder

Please Help wit java packages and Project Builder
Thread Tools
ptoomey3
Fresh-Faced Recruit
Join Date: Jun 2003
Status: Offline
Reply With Quote
Sep 9, 2003, 08:59 PM
 
I am trying to figure out how to use project builder while learning java. What I want to do should be very simple. I create a Java tool project called Hello. Hello.java is as follows:

import java.util.*;
import tools.fruit.*;
public class Hello {

public static void main (String args[]) {
Grape test = new Grape();
System.out.println("Hello World!");
}
}


I defined Grape.java elsewhere (outside the project) ~/javastuff/tools/fruit/Grape.java

The code for Grape.java is as follows:

package tools.fruit;
public class Grape {
public Grape() {
System.out.println ("Hello from Grape");
}

}


I compiled Grape.java into a class file...so I now have ~/javastuff/tools/fruit/Grape.class. I had to add ~/javastuff to my search paths "java classes" within the build settings within project builder before the Hello project would compile. I am fine up till this point...basically this changed my classpath to include ~/javastuff..thus allowing the compiler to find my Grape.class. However, once Hello tries to run I get:

Exception in thread "main" java.lang.NoClassDefFoundError: tools/fruit/Grape
at Hello.main(Hello.java:14)

Now, it seems to me that maybe the classpath I defined above only applied to compiling and not during running....because if I use the terminal and go into the build directory and run:
java -classpath ~/javastuff Hello the program
runs fine...able to load the grape class and executes as I would expect. How do you use simple packages in project builder? I have heard you can drop jar files into /Library/Java/Extensions...but I just want to have a directory full of class files all part of a package and be able to import and use them simply. Help!!!
Patrick
     
Michel Fortin
Forum Regular
Join Date: Jul 2001
Location: Qu�bec, Canada
Status: Offline
Reply With Quote
Sep 9, 2003, 09:54 PM
 
I suggest you look at the java executable (in the target tab) and edit launch arguements so that you pass the classpath arguments to java.
     
depolitic
Registered User
Join Date: Feb 2003
Status: Offline
Reply With Quote
Sep 9, 2003, 09:54 PM
 
If you are learning Java then Java IDE's are more of a pain then a blessing. You would be better of going with a simple text editor. Or something like JEdit see www.jedit.org or BlueJ.

As IDE's tend to hide's the fundamentals and add's a level of complexity into your learning the beginner does not need.

Just my thoughts.

Best of Luck.
     
ptoomey3  (op)
Fresh-Faced Recruit
Join Date: Jun 2003
Status: Offline
Reply With Quote
Sep 9, 2003, 10:14 PM
 
Hey,
I have actually used java enough that I've been through the whole command line and blueJ thing. I am now just wanting to try using project builder instead of vi for a change :-).

Anyway..I have made some progress in debugging the problem. I think it has to do with the fact that Project Builder compiles my Hello project into a jar file. I even tried passing the classpath arguement..still failed. I even went to the command line..manually set my CLASSPATH and then tried running the jar file from the command line..failure. If I dig into the project structure and find the class file and run that from the command line it is fine. So, I am convinced somehting is being lost upon creation of the jar file. I just started looking into this avenue and it makes mention of the manifest file. Do you know if would somehow encode a classpath within that somehow? Thanks again.
Patrick
     
Arkham_c
Mac Elite
Join Date: Dec 2001
Location: Atlanta, GA, USA
Status: Offline
Reply With Quote
Sep 10, 2003, 09:49 AM
 
That's just how classpath works. The easiest way to do it is to build a jar containing all your classes, then run the jar. You don't want to distribute unrolled class files anyway.

If you decide to go this route, look into ant. It's a really nice XML-based java make system. I use it at work to build all our EJB applications.
Mac Pro 2x 2.66 GHz Dual core, Apple TV 160GB, two Windows XP PCs
     
ptoomey3  (op)
Fresh-Faced Recruit
Join Date: Jun 2003
Status: Offline
Reply With Quote
Sep 10, 2003, 04:49 PM
 
Just to wrap up the thread...I finally discovered a solution (don't know if it is the best solution) to my problem. Basically what I found was that it was true that once project builder jars the class files together it does not take into account third party libraries. So, once the jar is run it can no longer find reference to packages that are located in an another directory. I tried moving the directory structure where my external package/classes were located into /Library/Java/Extensions to see if that helps...no use. Finally I bundled my classes in the external package into a jar and put the jar into /Library/Java/Extensions (you can also user ~/Library/Java/Extensions though you have to create that directory yourself). Once I did this project builder built and ran everything with no problem. Lastly..the class-path that exists in the manifest file can only reference relative pathnames ...so placing an absolute pathname to some external classes or jars is of no use. Thanks again.
Patrick
     
   
 
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 05:31 PM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,