1

I'm not exactly a total beginner with Java, but I'm at a loss for understanding what's going on here. When I try to compile the following code:

package controls;

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;

public class RButton  {
    public RButton() {

    }
}

I get the following error:

java.lang.NoClassDefFoundError: RButton (wrong name: controls/RButton)

The RButton.java file is located in the directory "project folder\ribbon\controls\". In the Ribbon folder I've been able to successfully put package ribbon; at the top of my source files there. I can't understand what I'm doing wrong here. Any help?

Edit: The problem lies with the batch file that I use in conjunction with Sublime Text for Java source files. JCreator has no problem compiling and running. Thanks for the help. though!

SimonT
  • 2,219
  • 1
  • 18
  • 32
  • Can you post the command you're using to compile (e.g. `javac ... `) – austin Apr 22 '13 at 20:13
  • Which is your source folder? – Sotirios Delimanolis Apr 22 '13 at 20:17
  • This suggests that the code is executed as `java RButton` instead of as `java controls.RButton`. Are you indeed executing in command console instead of in an IDE? Related/duplicate: http://stackoverflow.com/questions/7509295/noclassdeffounderror-wrong-name/7509317#7509317 – BalusC Apr 22 '13 at 20:19
  • I'm using Sublime Text. I suppose it might be a bad batch file that I took from some website, since by default Sublime Text will only compile but not run Java files. I wonder if you guys would happen to have a good batch file? :D – SimonT Apr 22 '13 at 20:21
  • If you use an IDE, it will make sure you place your files with the right paths and it will setup your class path so that all you need to do to compile and run your program is to press the `[Run]` button. – Peter Lawrey Apr 22 '13 at 20:21

1 Answers1

1

The package name for classes located in the controls folder should be ribbon.controls. The folder structure is matched 1 to 1 by the package naming and hierarchy.

So in the ribbon folder, you put package ribbon. In the ribbon/controls folder, you put package ribbon.controls. In the ribbon/controls/foo folder you use package ribbon.controls.foo, etc.

flavian
  • 28,161
  • 11
  • 65
  • 105
  • Changing the line to `package ribbon.controls;` gives the error `java.lang.NoClassDefFoundError: RButton (wrong name: ribbon/controls/RButton)`. – SimonT Apr 22 '13 at 20:15
  • Alex, I opened everything in JCreator and found that there was no problem. Your suggestion was indeed correct. However, it didn't solve the underlying issue- that my batch file for compiling Java source files had some sort of problem. I guess I'll have to use JCreator for this project. – SimonT Apr 22 '13 at 20:52