8 Replies - 480 Views - Last Post: 20 October 2012 - 10:05 AM Rate Topic: -----

#1 coldbreeze16  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 19-October 12

Exception in thread "main" error

Posted 19 October 2012 - 11:38 AM

Hello, I'm just starting with programming in Java with "Teach Yourself Java 6 in 21 Days" and I seem to have problems compiling this code I wrote for the exercise

import java.util.StringTokenizer;
class DateSplitter {

public static void main (String[] arguments) {
String dt = "21/08/1991";
StringTokenizer date = new StringTokenizer (dt, "/");
System.out.println ("Date :" + date.nextToken() + " Month :" + date.nextToken() + " Year :" + date.nextToken());
}


It compiles ok, but when executing the class file the cmd throws me this

D:\my java>java datebreaker
Exception in thread "main" java.lang.NoClassDefFoundError: datebreaker (wrong na
me: DateBreaker)
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(Unknown Source)
        at java.security.SecureClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.access$100(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)


Any help. please?

Is This A Good Question/Topic? 0
  • +

Replies To: Exception in thread "main" error

#2 coldbreeze16  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 19-October 12

Re: Exception in thread "main" error

Posted 19 October 2012 - 11:44 AM

err isn't there an edit button for posts? I meant I get the error when running, not when compiling. And I tried changing the class name to datebreaker, I posted the error message for that instead of the original. Same errors though.
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Exception in thread "main" error

Posted 19 October 2012 - 12:13 PM

The fiolename should exactly match the class name
so the code for class DateSplitter should be in file DateSplitter.java
Was This Post Helpful? 1
  • +
  • -

#4 coldbreeze16  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 19-October 12

Re: Exception in thread "main" error

Posted 19 October 2012 - 12:35 PM

View Postpbl, on 19 October 2012 - 12:13 PM, said:

The fiolename should exactly match the class name
so the code for class DateSplitter should be in file DateSplitter.java


Err thanks I knew that, it seems that I'd left out a 't' in the class name
Was This Post Helpful? 0
  • +
  • -

#5 coldbreeze16  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 19-October 12

Re: Exception in thread "main" error

Posted 19 October 2012 - 12:51 PM

I'm having the same problem with another proggy and I've double checked the filenames this time :( .The problem was to make a class with 3 int values and then, in another class, instantiate the 1st class, assign the int values to objects and then retrieve them.

class Dimensions {
int ht=12, wt=1200, dp=2;
}


class Book {
public static void main (String[] arguments) {
Dimensions Textbook = new Dimensions();
Integer height = new Integer(Textbook.ht);
Integer weight = new Integer(Textbook.wt);
Integer depth = new Integer(Textbook.dp);
System.out.println ("The retrieved values are :" + height.intValue() + weight.intValue() + depth.intValue());
}
}


What am I doing wrong here?
Was This Post Helpful? 0
  • +
  • -

#6 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2197
  • View blog
  • Posts: 5,224
  • Joined: 10-September 10

Re: Exception in thread "main" error

Posted 19 October 2012 - 01:15 PM

You might consider adding a constructor to the first class; then maybe getter and setter methods, though your explanation of the assignment is a bit thin.
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Exception in thread "main" error

Posted 19 October 2012 - 07:04 PM

Should work
No reason for not working if you have a Dimensions.java and a Book.java

Post the error you have
Was This Post Helpful? 0
  • +
  • -

#8 coldbreeze16  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 19-October 12

Re: Exception in thread "main" error

Posted 19 October 2012 - 10:18 PM

This is the assignment given

Quote

Create a class with instance variables for height, weight, and depth, making each an integer. Create a Java application that uses your new class, sets each of these values in an object, and displays the values.


I tried changing the code a bit by not initializing the ht,wt and dp as class variables but fed them values as instance variables. The error I get is the same, Exception in thread "main"... Here is a screencap that shows the error as well as the filenames Posted Image
Was This Post Helpful? 0
  • +
  • -

#9 coldbreeze16  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 19-October 12

Re: Exception in thread "main" error

Posted 20 October 2012 - 10:05 AM

The solution was incredibly stupid, but then the origins of most problems are extremely simple. The command prompt on NT system is case insensitive, so I can compile my src with
javac book.java

but running it with
java book
would obviously fail because my class's name is Book and not book, I'm calling for the class and not the file Book.java . And all the while I was setting and resetting my CLASSPATH :|
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1