views:

452

answers:

2

Why am I getting this error when the SystemController class is in the same directory?

sgs$ javac Main.java 

Main.java:27: cannot find symbol
symbol  : class SystemController
location: class sgs.Main
        SystemController sc = new SystemController();
        ^
Main.java:27: cannot find symbol
symbol  : class SystemController
location: class sgs.Main
        SystemController sc = new SystemController();
                                  ^
2 errors


package sgs;

import javax.swing.JFrame;

public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here

        boolean loginSuccess = false;
        //Login login = new Login();
        //login.setVisible(true);
        //login.loadAccounts("files/accounts.txt");

        SystemController sc = new SystemController();
    sc.setVisible(true);
        sc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

}
+1  A: 

Have you compiled SystemController ?

try

javac *.java

Also, remember to specify your classpath where the Swing classes are.

javac -cp classpath *.java
Tom
javac *.javaNote: Login.java uses or overrides a deprecated API.Note: Recompile with -Xlint:deprecation for details.
Although that compiled everything, but it still get the same error stated in my original post
+2  A: 

My guess is that you didn't compile SystemController on which Main depends. So either compile manually SystemController before to compile Main (but that will be painful on the long term if the number of classes grows) or compile everything together and let the compiler calculates the compilation order (better IMO). Something like this:

$ pwd
/path/to/sgs
$ cd ..
$ javac sgs/*.java
$ java -cp . sgs.Main

EDIT: From the error you posted as comment, I can see that you are using GNU GCJ which doesn't fully support Swing. Please switch to Sun JDK or OpenJDK. Both should be available as package, just make sure to make it the default Java after install (see https://help.ubuntu.com/community/Java for Ubuntu or a Debian based distro, find out how to do this for another distro).

Pascal Thivent
java sgs.MainException in thread "main" java.lang.NoClassDefFoundError: sgs.SystemController at java.lang.Class.initializeClass(libgcj.so.10) at sgs.Main.main(Main.java:27)Caused by: java.lang.ClassNotFoundException: javax.swing.GroupLayout not found in gnu.gcj.runtime.SystemClassLoader{urls=[file:./], parent=gnu.gcj.runtime.ExtensionClassLoader{urls=[], parent=null}} at java.net.URLClassLoader.findClass(libgcj.so.10) at gnu.gcj.runtime.SystemClassLoader.findClass(libgcj.so.10) at java.lang.ClassLoader.loadClass(libgcj.so.10) at java.lang.ClassLoader.loadClass(libgcj.so.10)
I'm using netbeans.... it compiles fine from the IDE
I think that Netbeans uses its own JDK which is not the same that you are using on the command line (you are using `gcj`). Typing `java -version` in a console would confirm this. I've edited my answer to provide more guidance.
Pascal Thivent
OK so I updated my java version.java version "1.6.0_16"Java(TM) SE Runtime Environment (build 1.6.0_16-b01)Java HotSpot(TM) Server VM (build 14.2-b01, mixed mode)
However when I compile all java files get a .class file, but the SystemController.java file gets 13 different class files.
Because it's a Swing class created by a designer and it contains many inner classes which is very common in Swing code. But... so what? :)
Pascal Thivent