This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes 2 Questions - I'm really at a loss! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "2 Questions - I Watch "2 Questions - I New topic
Author

2 Questions - I'm really at a loss!

Gary Farms
Ranch Hand

Joined: Jun 24, 2000
Posts: 57
Below is my program Access.java which resides in c:\java.
It generates a "NoClassDefFoundError" when I run it. I have No idea why. Also, I've always had to put a package statement
at the top of all my .java files. Otherwise, they will compile, but won't run. To run Access.java, I compile it, then copy
Access.class to c:\com\gfarms\geometry then run it at c:\java
by doing java com.gfarms.geometry.Access
My 2 questions are:
1. Why do I get this "NoClassDefFoundError" when I run Acces.java?
2. What is wrong with my classpath, that requires me to put a package statement in all my .java files?
Anyway, here's my path and classpath statements in c:\autoexec.bat
path=c:\windows;c:\windows\command;c:\jdk1.3.0_02\bin;c:\java\;
set classpath=c:\jdk1.3.0_02\lib;c:\java\;
Finally, here's my c:\java\Access.java program that blows up:
//Access.java
package com.gfarms.geometry;
class Other
{
public void fnc(String b)
{
int y = 10;
System.out.println("string received = " + b);
System.out.println("y = " + y);
}
}
public class Access
{
public static void main(String[] args)
{
Other a = new Other();
a.fnc("Gary Farms");
}
}
Any help to my 2 above questions would be deeply appreciated!
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Gary,
One thing you should make sure is that your classpath includes the current directory. This way you can run any java program from the directory were it is located without the need for package statements.
set classpath=.;%CLASSPATH%;
Regards,
Manfred.
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Gary,
Did you read that other link that I gave you yesterday. It talks about exactly this.
YOU MUST HAVE A DRIVER CLASS IN THE CLASSPATH TO START THING OFF.
Access.class is your driver. It is the class with the main. It MUST be in the direct classpath, NOT in a sub-directory (package) or nothing will work. Put it in c:/java with no package statement at the top.
Your other classes, like Other you can either leave in the c:/java directory with no package statement and it will work just fine with no import statements.
OR you can put it in a lower subdirectory (package), and import it into the classes that use it (in this case Access). This is not an option with the Access class itself.
PS. Learn to use "Reply to Post" on your own threads so that you do not fragment your conversations this way.


"JavaRanch, where the deer and the Certified play" - David O'Meara
Gary Farms
Ranch Hand

Joined: Jun 24, 2000
Posts: 57

Cindy:
I read all your replies and followed your advice. Now, I get
a compile error when I compile c:\java\Access.java
I removed the package statement from Access.java (code is at top). Access.java still contains the Access class and the Other class. All I did was follow your advice and remove the package statement. I have Access.java stored at c:\java and compile it there. I now get the following compile error:
Access.java:17 fnc(String) in Other cannot be applied to (java.lang.String) a.fnc("Gary Farms");
Why?
My classpath statement looks as follows:
set classpath=c:\jdk1.3.0_02\lib;c:\java\;
Why do I get this compile error? Is there anything now wrong with having these 2 classes in this c:\java\Access.java file?
Is my claspath statement OK?

Originally posted by Cindy Glass:
Gary,
Did you read that other link that I gave you yesterday. It talks about exactly this.
YOU MUST HAVE A DRIVER CLASS IN THE CLASSPATH TO START THING OFF.
Access.class is your driver. It is the class with the main. It MUST be in the direct classpath, NOT in a sub-directory (package) or nothing will work. Put it in c:/java with no package statement at the top.
Your other classes, like Other you can either leave in the c:/java directory with no package statement and it will work just fine with no import statements.
OR you can put it in a lower subdirectory (package), and import it into the classes that use it (in this case Access). This is not an option with the Access class itself.
PS. Learn to use "Reply to Post" on your own threads so that you do not fragment your conversations this way.

Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879

I ran your code(minus the package statement) and it worked fine for me???
Jamie

[This message has been edited by Jamie Robertson (edited August 01, 2001).]
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
This error message:
Access.java:17 fnc(String) in Other cannot be applied to (java.lang.String) a.fnc("Gary Farms");
Implies that there is ANOTHER String class that is NOT in java.lang and when you compiled Other it used the other String class, so when you try to use an instance of java.lang.String as a parameter to the method it spits up.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: 2 Questions - I'm really at a loss!
 
Similar Threads
NoClassDefFoundError
2 Questions - I'm Really At A Loss!
NoClassDefFoundError
Followup To Cindy Glass Reply
HelloWorld NoClassDefFoundError