Page 1 of 2 12 LastLast
Results 1 to 20 of 28
  1. #1
    Join Date
    Oct 2010
    Posts
    12
    Rep Power
    0

    Default javac "Cannot find symbol"

    Hello.

    Im struggeling with java packages.

    I have 2 files:

    C:\Users\admin\Documents\NetBeansProjects\JavaAppl ication10\src\javaapplication10\Main.java

    Java Code:
    package javaapplication10;
    
    
    
    /**
     *
     * @author admin
     */
    public class Main {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            // TODO code application logic here
    
            say say = new say();
            System.out.print(say.hello() + "\n");
        }
    
    }
    And


    C:\Users\admin\Documents\NetBeansProjects\JavaAppl ication10\src\javaapplication10\say.java

    Java Code:
    package javaapplication10;
    
    /**
     *
     * @author admin
     */
    public class say {
    
        public String hello()
        {
            String msg = "hello";
            return msg;
        }
        public String goodbye()
        {
            String msg = "goodbye";
            return msg;
        }
    }
    when i try to compile i get the following error:

    Java Code:
    PS C:\Users\admin\Documents\NetBeansProjects\JavaApplication10\src\javaapplication10> javac Main.java
    Main.java:22: cannot find symbol
    symbol  : class say
    location: class javaapplication10.Main
            say say = new say();
            ^
    Main.java:22: cannot find symbol
    symbol  : class say
    location: class javaapplication10.Main
            say say = new say();
                          ^
    2 errors
    If i comment the package declaration out. i can compile and run the files fine without any problems. i dont understand this. any help is greatly appriciated.

    /mads

  2. #2
    tashimoto is offline Member
    Join Date
    Sep 2010
    Location
    Oregon, usa
    Posts
    69
    Rep Power
    0

    Default

    Your code worked on my system, and I'm also using NetBeans. So I'm sure your code is correct. :)

    I did a quick search on NetBeans for similar cases, and I found a thread that might be useful: NetBeans Forums - Netbeans claims it cannot find "symbol"

    Skip down to Damian's response about clearing the cache. Maybe that is the same thing you are experiencing. Good Luck!

    Chris

  3. #3
    Join Date
    Oct 2010
    Posts
    12
    Rep Power
    0

    Default

    I dont know how this shouldt affect the "compiler" as i get this error at compile time.
    Anyway i did "clear" the cache. the problem persists.

    /mads

  4. #4
    Join Date
    Oct 2010
    Posts
    12
    Rep Power
    0

    Default

    And i only get this "error" when i use package. if i comment the package declaration out. it compiles and runs fine.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default

    Go up a level and compile from there.
    I think the compiler is looking for "say" in a javaapplication10 from where you are running javac, so it can't find it.

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,563
    Rep Power
    23

    Default

    When using packages, the class path for the javac command must point to the folder containing the package folder. You can set the classpath up one level by using a classpath something like this:
    -classpath .;..\.
    Current folder and up one level

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default

    That's the other option...;)

  8. #8
    Join Date
    Oct 2010
    Posts
    12
    Rep Power
    0

    Default

    I can now compile if i follow Norm's advice, compiling from "elevated ground" :)

    But now i get a new error:

    PS C:\Users\admin\Documents\NetBeansProjects\JavaAppl ication10\src\javaapplication10> java Main.java
    Exception in thread "main" java.lang.NoClassDefFoundError: Main/java
    Caused by: java.lang.ClassNotFoundException: Main.java
    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)
    Could not find the main class: Main.java. Program will exit.
    PS C:\Users\admin\Documents\NetBeansProjects\JavaAppl ication10\src\javaapplication10>

    Again. this works fine without the package declaration.

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,000
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by madsovenielsen View Post
    I can now compile if i follow Norm's advice, compiling from "elevated ground" :)

    But now i get a new error:

    PS C:\Users\admin\Documents\NetBeansProjects\JavaAppl ication10\src\javaapplication10> java Main.java
    Exception in thread "main" java.lang.NoClassDefFoundError: Main/java
    Caused by: java.lang.ClassNotFoundException: Main.java
    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)
    Could not find the main class: Main.java. Program will exit.
    PS C:\Users\admin\Documents\NetBeansProjects\JavaAppl ication10\src\javaapplication10>

    Again. this works fine without the package declaration.
    Your class is not named 'Main' it is named 'javaapplication10.Main'.

    kind regards,

    Jos

  10. #10
    Join Date
    Oct 2010
    Posts
    12
    Rep Power
    0

    Default

    package javaapplication10;



    /**
    *
    * @author admin
    */
    public class Main {

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

    say say = new say();
    System.out.print(say.hello() + "\n");
    }

    }

    My class is named Main.

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default

    It's in the javaapplication10 package, which means it is called javaapplication10.Main.
    That's how java naming works.

  12. #12
    Join Date
    Oct 2010
    Posts
    12
    Rep Power
    0

    Default

    package javaapplication10;



    /**
    *
    * @author admin
    */
    public class javaapplication10.Main {

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

    say say = new say();
    System.out.print(say.hello() + "\n");
    }

    }

    I get an error in NetBeans and i get the following error when i try to compile:

    javaapplication10\Main.java:14: '{' expected
    public class javaapplication10.Main {
    ^
    1 error

  13. #13
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,000
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by madsovenielsen View Post
    package javaapplication10;



    /**
    *
    * @author admin
    */
    public class javaapplication10.Main {

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

    say say = new say();
    System.out.print(say.hello() + "\n");
    }

    }

    I get an error in NetBeans and i get the following error when i try to compile:

    javaapplication10\Main.java:14: '{' expected
    public class javaapplication10.Main {
    ^
    1 error
    You should read some tutorials first before you drown in classpath issues, compilation problems and your own incorrect fantasy.

    kind regards,

    Jos

  14. #14
    Join Date
    Oct 2010
    Posts
    12
    Rep Power
    0

    Default

    My code is now like this:

    Main.java
    Java Code:
    package javaapplication10.Main;
    
    import javaapplication10.say.say;
    
    /**
     *
     * @author admin
     */
    public class Main {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            // TODO code application logic here
    
            say say = new say();
            System.out.print(say.hello() + "\n");
        }
    
    }
    say.java
    Java Code:
    package javaapplication10.say;
    
    /**
     *
     * @author admin
     */
    public class say {
    
        public String hello()
        {
            String msg = "hello";
            return msg;
        }
        public String goodbye()
        {
            String msg = "goodbye";
            return msg;
        }
    }
    The files is in the directories:

    say and Main

    i can compile say.java fine. but when i try to compile Main.java get the following error:

    Main\Main.java:8: package javaapplication10.say does not exist
    import javaapplication10.say.say;
    ^
    Main\Main.java:22: cannot find symbol
    symbol : class say
    location: class javaapplication10.Main.Main
    say say = new say();
    ^
    Main\Main.java:22: cannot find symbol
    symbol : class say
    location: class javaapplication10.Main.Main
    say say = new say();
    ^
    3 errors

    I have no errors in NetBeans and i have looked through this "tutorial"

    The Java Package Tutorial

    Any help is greatly appriciated.

  15. #15
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,000
    Blog Entries
    7
    Rep Power
    20

    Default

    Now you have two packages: javaapplication10.Main and javaApplication10.say; my guess is that it's not what you want. Study those tutorials and don't be in a hurry.

    kind regards,

    Jos

  16. #16
    Join Date
    Oct 2010
    Posts
    12
    Rep Power
    0

    Default

    I have now read this one to:
    Java - Packages

    No help either. i just can't understand this.

    Both files, say.java and Main.java reside in the _same_ directory.

  17. #17
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,563
    Rep Power
    23

    Default

    java Main.java
    The java command is for executing a program.
    The javac command is for compiling a program.

    In post#8 You are using the wrong command to compile. Use javac

    Use: java Main (no file extension)
    to execute a class.

  18. #18
    Join Date
    Oct 2010
    Posts
    12
    Rep Power
    0

    Default

    Im sorry. it was a bit unclear.

    I have compiled the code succesfully. but when i try to run it i get this error:

    Java Code:
    PS C:\Users\admin\Documents\NetBeansProjects\JavaApplication10\src\javaapplication10> ls
    
    
        Directory: C:\Users\admin\Documents\NetBeansProjects\JavaApplication10\src\javaapplication10
    
    
    Mode                LastWriteTime     Length Name
    ----                -------------     ------ ----
    -a---        07-10-2010     07:16        646 Main.class
    -a---        06-10-2010     21:33        415 Main.java
    -a---        07-10-2010     07:16        339 say.class
    -a---        06-10-2010     21:09        359 say.java
    
    
    PS C:\Users\admin\Documents\NetBeansProjects\JavaApplication10\src\javaapplication10> java Main
    Exception in thread "main" java.lang.NoClassDefFoundError: Main (wrong name: javaapplication10/Main)
            at java.lang.ClassLoader.defineClass1(Native Method)
            at java.lang.ClassLoader.defineClassCond(Unknown Source)
            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$000(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)
    Could not find the main class: Main.  Program will exit.
    PS C:\Users\admin\Documents\NetBeansProjects\JavaApplication10\src\javaapplication10>
    Again. if i remove the package declaration it compiles and runs fine.

    Any help is greatly appriciated

  19. #19
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    I'm not totally with this thread, but looking at the last error, are you sure that the package is exist physically that you've mentioned in the class?

  20. #20
    Join Date
    Oct 2010
    Posts
    12
    Rep Power
    0

    Default

    As i understand packages. after reading these tutorials:

    Java - Packages
    The Java Package Tutorial

    A package is just a folder that contains the files. in my situation: javaapplication10

    And yes the folder exists. :)

Page 1 of 2 12 LastLast

Similar Threads

  1. "Cannot find symbol" errors in Java
    By 23Zone in forum New To Java
    Replies: 1
    Last Post: 02-17-2010, 07:13 AM
  2. Compiling probem "cannot find symbol"
    By thegluups in forum New To Java
    Replies: 27
    Last Post: 01-18-2010, 08:53 PM
  3. Error "can not find symbol variable"
    By FullMetalHollow in forum New To Java
    Replies: 5
    Last Post: 10-04-2009, 09:51 PM
  4. cannot find symbol for "list.addFirst"
    By alexbryan_08 in forum New To Java
    Replies: 10
    Last Post: 08-26-2009, 08:55 AM
  5. "Cannont find symbol Constructor" error
    By Welsh in forum New To Java
    Replies: 7
    Last Post: 01-25-2008, 12:12 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •