Results 1 to 20 of 28
Thread: javac "Cannot find symbol"
- 10-05-2010, 08:20 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 12
- Rep Power
- 0
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
AndJava 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"); } }
C:\Users\admin\Documents\NetBeansProjects\JavaAppl ication10\src\javaapplication10\say.java
when i try to compile i get the following error: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; } }
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.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
/mads
- 10-05-2010, 08:56 PM #2
Member
- Join Date
- Sep 2010
- Location
- Oregon, usa
- Posts
- 69
- Rep Power
- 0
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
- 10-06-2010, 01:44 PM #3
Member
- Join Date
- Oct 2010
- Posts
- 12
- Rep Power
- 0
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
- 10-06-2010, 01:53 PM #4
Member
- Join Date
- Oct 2010
- Posts
- 12
- Rep Power
- 0
And i only get this "error" when i use package. if i comment the package declaration out. it compiles and runs fine.
- 10-06-2010, 03:22 PM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
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.
- 10-06-2010, 03:56 PM #6
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
- 10-06-2010, 04:16 PM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
That's the other option...;)
- 10-06-2010, 05:51 PM #8
Member
- Join Date
- Oct 2010
- Posts
- 12
- Rep Power
- 0
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.
- 10-06-2010, 06:08 PM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
- 10-06-2010, 06:35 PM #10
Member
- Join Date
- Oct 2010
- Posts
- 12
- Rep Power
- 0
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.
- 10-06-2010, 06:43 PM #11
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
It's in the javaapplication10 package, which means it is called javaapplication10.Main.
That's how java naming works.
- 10-06-2010, 06:58 PM #12
Member
- Join Date
- Oct 2010
- Posts
- 12
- Rep Power
- 0
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
- 10-06-2010, 07:01 PM #13
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
- 10-06-2010, 07:34 PM #14
Member
- Join Date
- Oct 2010
- Posts
- 12
- Rep Power
- 0
My code is now like this:
Main.java
say.javaJava 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"); } }
The files is in the directories: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; } }
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.
- 10-06-2010, 07:42 PM #15
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
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
- 10-06-2010, 08:14 PM #16
Member
- Join Date
- Oct 2010
- Posts
- 12
- Rep Power
- 0
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.
- 10-06-2010, 09:55 PM #17
The java command is for executing a program.java Main.java
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.
- 10-07-2010, 06:26 AM #18
Member
- Join Date
- Oct 2010
- Posts
- 12
- Rep Power
- 0
Im sorry. it was a bit unclear.
I have compiled the code succesfully. but when i try to run it i get this error:
Again. if i remove the package declaration it compiles and runs fine.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>
Any help is greatly appriciated
- 10-07-2010, 06:33 AM #19
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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?
- 10-07-2010, 07:02 AM #20
Member
- Join Date
- Oct 2010
- Posts
- 12
- Rep Power
- 0
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. :)
Similar Threads
-
"Cannot find symbol" errors in Java
By 23Zone in forum New To JavaReplies: 1Last Post: 02-17-2010, 07:13 AM -
Compiling probem "cannot find symbol"
By thegluups in forum New To JavaReplies: 27Last Post: 01-18-2010, 08:53 PM -
Error "can not find symbol variable"
By FullMetalHollow in forum New To JavaReplies: 5Last Post: 10-04-2009, 09:51 PM -
cannot find symbol for "list.addFirst"
By alexbryan_08 in forum New To JavaReplies: 10Last Post: 08-26-2009, 08:55 AM -
"Cannont find symbol Constructor" error
By Welsh in forum New To JavaReplies: 7Last Post: 01-25-2008, 12:12 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks