Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Mar 20, 2007, 10:51 AM   #1
jsprice2
macrumors newbie
 
Join Date: Nov 2006
Beginning Java programmer 10.4

java beginner - OS 10.4 - using terminal and Head First Java text to self learn - old BASIC person from years ago -

get the following error:
Exception in thread "main" java.lang.NoClassDefFoundError: GoodDogTestDrive/class

code below - would also like to know how to run code with 2 classes - everything is in the same directory
-------------

class GoodDog {
private int size;

public int getSize() {
return size;
}
public void setSize(int s) {
size = s;
}
void bark() {
if (size > 60) {
System.out.println("Woof Woof !");
} else if (size > 14) {
System.out.println("Ruff Ruff!");
} else {
System.out.println("Yip Yip!");
}
}
}

class GoodDogTestDrive {
public static void main (String[ ] args) {
GoodDog one = new GoodDog();
one.setSize(70);

GoodDog two = new GoodDog();
two.setSize(8);

System.out.println("Dog one: " + one.getSize());
System.out.println("Dog two: " + two.getSize());

one.bark();
two.bark();
}
}
jsprice2 is offline   0 Reply With Quote
Old Mar 20, 2007, 11:00 AM   #2
lazydog
macrumors 6502a
 
Join Date: Sep 2005
Location: Cramlington, UK
Send a message via MSN to lazydog
Is your file named GoodDogTestDrive.java?

b e n

EDIT: I've put all the code you posted in a file called GoodDogTestDrive.java and it compiles and runs fine.

Last edited by lazydog; Mar 20, 2007 at 11:24 AM.
lazydog is offline   0 Reply With Quote
Old Mar 20, 2007, 11:14 AM   #3
cbougher
macrumors member
 
Join Date: Oct 2006
Location: Atlanta, GA
I think GoodDogTestDrive needs to be declared public.
cbougher is offline   0 Reply With Quote
Old Mar 20, 2007, 11:30 AM   #4
ChrisBrightwell
macrumors 68020
 
ChrisBrightwell's Avatar
 
Join Date: Apr 2004
Location: Huntsville, AL
Don't include the ".class" in your java command.

For example, to build and run HelloWorld.java:

Code:
class HelloWorld
{
  public static void main (String args[])  
  {
    System.out.println("Hello, world!");
  }
}
Then in terminal, these are your commands:
Code:
% javac HelloWorld.java
% java -cp ./ HelloWorld
The problem with using "java HelloWorld.class" is that the . is a package/file separator, so it looks for ./HelloWorld/class.class ... which won't exist.

Also, the "-cp ./" is just to ensure that it looks in the local directory for the .class file to run. Some JVMs add automatically search there, but not all do, so it's usually a good practice to explicitly set the classpath.

Hope this helps.
__________________
"Simplicity is the ultimate sophistication."
ChrisBrightwell is offline   0 Reply With Quote
Old Mar 20, 2007, 02:27 PM   #5
scan
macrumors 6502
 
Join Date: Oct 2005
Quote:
Originally Posted by ChrisBrightwell View Post
Don't include the ".class" in your java command.

For example, to build and run HelloWorld.java:

Code:
class HelloWorld
{
  public static void main (String args[])  
  {
    System.out.println("Hello, world!");
  }
}
Then in terminal, these are your commands:
Code:
% javac HelloWorld.java
% java -cp ./ HelloWorld
The problem with using "java HelloWorld.class" is that the . is a package/file separator, so it looks for ./HelloWorld/class.class ... which won't exist.

Also, the "-cp ./" is just to ensure that it looks in the local directory for the .class file to run. Some JVMs add automatically search there, but not all do, so it's usually a good practice to explicitly set the classpath.

Hope this helps.
exactly what he said. This is a common exception. If you run your program without the .class then its a classpath issue.
scan is offline   0 Reply With Quote
Old Mar 21, 2007, 10:56 AM   #6
jsprice2
Thread Starter
macrumors newbie
 
Join Date: Nov 2006
Beginning Java programmer 10.4

Thanks for alll the help. I did put .class at the end of command line and it was the problem.
jsprice2 is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Some apps prompt to install Java 6 even when Java 7 is installed MacMan988 OS X Mavericks (10.9) 1 Apr 18, 2014 07:56 AM
Java 6 or Java 7 for OSX Mavericks for your Macbook Pro? Starfyre MacBook Pro 3 Dec 22, 2013 03:49 PM
Java 6.x security risk (?) vs Java 7.x and broken apps w/ v7.x installs? mgiamo Mac Basics and Help 1 Dec 15, 2013 03:48 PM
Chrome on OS X -- Java plugin / Java 7 vs Java 6 Issue booyahbooyah Mac Applications and Mac App Store 4 May 31, 2013 09:13 AM
Installed Java 7.07 but doesn't show up in Java preferences? -mattias- OS X 10.8 Mountain Lion 21 Aug 31, 2012 04:49 PM

Forum Jump

All times are GMT -5. The time now is 06:28 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC