• Post Reply Bookmark Topic Watch Topic
  • New Topic

Exception compiling with javac  RSS feed

 
Catalin Dumitrache
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everybody, I'm new to this forum and I have a quick question...

I'm trying to compile a bunch of .java-s through a terminal, with "javac Main.java" and I get a "Cannot find symbol" exception.

I use to program with Eclipse, but for this project it had to compile through terminal, so I don't know the difference, because it works fine with Eclipse.

The thing is that I have split the project in different .java files, each one with a class. The class Partida is in the carpet with Main.java, but still it isn't recognized.

Thank you for the help.



Here's the code:




And this is the exception:

 
Greg Charles
Sheriff
Posts: 3015
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Catalin, welcome to Java Ranch!

If all the Java files are in the same directory, the easiest thing would be just to compile them all at once. Like:

javac *.java

When you start organizing things into packages, then you will have to learn about class paths, which can be tricky for beginners. Build tools like Ant, or an IDE like Eclipse or IntelliJ can help, but it's good to understand the basics first.

Also, I have to ask what word you translated into "carpet"? I hope it's clear I'm asking out of curiosity, not pettiness. I think the word you meant was directory or folder, but I'm not completely sure.
 
Catalin Dumitrache
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greg Charles wrote:Hi Catalin, welcome to Java Ranch!

If all the Java files are in the same directory, the easiest thing would be just to compile them all at once. Like:

javac *.java


Well, I just tried doing that and it fixed those 2 problems, but other 5 appeared instead:



Greg Charles wrote:
Also, I have to ask what word you translated into "carpet"? I hope it's clear I'm asking out of curiosity, not pettiness. I think the word you meant was directory or folder, but I'm not completely sure.


You are right, I ment directory, my English is very bad, sorry for that xD
 
Liutauras Vilda
Sheriff
Posts: 4918
334
BSD
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Lets start over.
1. How many files do you have in the directory where you executing "javac *.java" command?
2. How the files are named? How your classes are defined within your files?
3. How many defined classes do you have per source (.java) files?
4. Does your classes contains any "package" statements?
 
Greg Charles
Sheriff
Posts: 3015
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Catalin Dumitrache wrote:
You are right, I ment directory, my English is very bad, sorry for that xD


Well, it's a lot better than my Spanish! Anyway, I wasn't trying to criticize. I just found that an interesting substitution, like maybe there's a Spanish word that means both directory and carpet. I'm weirdly fascinated with languages.

I'm surprised javac * didn't work for you, but Liutaurus brings up some good points. I can't actually think of a scenario where all the java files are in the same directory, but the compiler fails that way, but if you give us some more information, we can probably figure it out.
 
Catalin Dumitrache
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:Hi,

Lets start over.
1. How many files do you have in the directory where you executing "javac *.java" command?
2. How the files are named? How your classes are defined within your files?
3. How many defined classes do you have per source (.java) files?
4. Does your classes contains any "package" statements?


Hi !!

1. I have only 4 .java files
2. There is "Main.java", "Carta.java", "Partida.java" and "Baraja.java". Each and everyone of theclasses are named like their file name (public class Main.., public class Carta.., etc)
3. Only one class per .java
4. They did, untill someone told me it's unnecessary if you compile through terminal. I have all of those files in a directory called "java" and as there are no subdirectories I think no package statement is needed, right ?
 
Catalin Dumitrache
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greg Charles wrote:
Catalin Dumitrache wrote:
You are right, I ment directory, my English is very bad, sorry for that xD


Well, it's a lot better than my Spanish! Anyway, I wasn't trying to criticize. I just found that an interesting substitution, like maybe there's a Spanish word that means both directory and carpet. I'm weirdly fascinated with languages.

That's right, there is the word "directorio" which means directory, but there is also a word "carpeta" which also means directory.

Greg Charles wrote:I'm surprised javac * didn't work for you, but Liutaurus brings up some good points. I can't actually think of a scenario where all the java files are in the same directory, but the compiler fails that way, but if you give us some more information, we can probably figure it out.


Okey, let's see if we can solve this haha, it's really giving me a headache :S
 
Liutauras Vilda
Sheriff
Posts: 4918
334
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Baraja.java:50: cannot find symbol
symbol : variable Main
location: class practica1Java.Baraja
indice=Main.rand();

1. Check Baraja class and show us, how did you create and instance of your "Main" class?
2. Does your "rand" method has "static" non access modifier?
3. Did you recompile that class? "location: class practica1Java.Baraja" as it seems it is packaged (you mentioned it is not)
 
Catalin Dumitrache
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
New update:

- I had a line uncommented so that's why "javac *.java" didn't work at first, but still, the problem when compiling with "javac Main.java" persists.

- When compiling with "javac *.java", I try to execute with "java Main" and I get this exception:

 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If that class Partida has a package name, try copying it into the same directory as all the other .java files and removing the package name.
Also show us the result of printing the contents of the current directory with ls (Linux) or dir (Windows). Be sure to use copy‑and‑paste so we can see any spelling errrors.
 
Catalin Dumitrache
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:
Baraja.java:50: cannot find symbol
symbol : variable Main
location: class practica1Java.Baraja
indice=Main.rand();

1. Check Baraja class and show us, how did you create and instance of your "Main" class?
2. Does your "rand" method has "static" non access modifier?
3. Did you recompile that class? "location: class practica1Java.Baraja" as it seems it is packaged (you mentioned it is not)


1. In Baraja I don't have any instance of Main class, I only make use of rand method:


2. This is the "rand" part... My professor gave me this part of the code and to be honest I don't understand very well the modifiers... I know static is so you can't use it on class objects


3. Yeah, I recently commented all the package lines (I thought they were already) , and javac *.java works now.
 
Catalin Dumitrache
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:If that class Partida has a package name, try copying it into the same directory as all the other .java files and removing the package name.
Also show us the result of printing the contents of the current directory with ls (Linux) or dir (Windows). Be sure to use copy‑and‑paste so we can see any spelling errrors.


- Class Partida is already in the same directory and no package statement

- Here's the directory, after compiling with javac *.java
 
Liutauras Vilda
Sheriff
Posts: 4918
334
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, run the program "java [class]", the one which has "main" method.
 
Catalin Dumitrache
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Solved, you just have to use the -cp option (my professor's fault, because he told us we couldn't use it)
Thanks to everybody anyway.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have to use the -cp option, then the problem is not solved. It suggests you have set a system CLASSPATH, which is usually a bad idea and usually does more harm than good. Older books tell you how to set it, but it is not necessary any more. Please show us the contents of your CLASSPATH with this instruction:-
echo %CLASSPATH%
 
Catalin Dumitrache
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:If you have to use the -cp option, then the problem is not solved. It suggests you have set a system CLASSPATH, which is usually a bad idea and usually does more harm than good. Older books tell you how to set it, but it is not necessary any more. Please show us the contents of your CLASSPATH with this instruction:-
echo %CLASSPATH%



 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought you were using a Windows box, which is why I said %CLASSPATH% rather than $CLASSPATH.
Where did you declare that CLASSPATH? Was it in .bashrc or similar?
Why have you got it in the first place?

O good grief! It has Oracle in. Have you installed an old version of Oracle? Oracle used to be notorious for changing the system CLASSPATH while you weren't watching.
 
Catalin Dumitrache
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I thought you were using a Windows box, which is why I said %CLASSPATH% rather than $CLASSPATH.
Where did you declare that CLASSPATH? Was it in .bashrc or similar?
Why have you got it in the first place?

O good grief! It has Oracle in. Have you installed an old version of Oracle? Oracle used to be notorious for changing the system CLASSPATH while you weren't watching.


Sincerely no ideahow I declared that CLASSPATH, I don't remember defining or touching it :S
And neither Oracle.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you don't have Oracle, start looking for that CLASSPATH. Try in ~/.bashrc first. If not try in /etc/environment (root or sudo access required). If you find it, comment it out and add a comment to explain what you are doing. Comments start with #, so you will end up with a file looking something like this
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games"
export GTK_IM_MODULE="xim"
# Removed 8th August 2015 because oracle classpath unnecessary
# CLASSPATH="/usr/local/oracle/app/oracle/product/9.0.1/jdbc/lib/classes12.jar:/usr/local/oracle/app/oracle/product/9.0.1/jdbc/lib/nls_charset12.jar:/usr/local/oracle/app/oracle/product/9.0.1/jdbc/lib/ocrs12.jar"
You will probably not have the GTK module line. The # is a bit like // and must be applied to each line.

If you ever have any problems, you can consider restoring the cLASSPATH by removing the #
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!