Strauss
Strauss

Reputation: 43

error : cannot find symbol with Javac shows up when everything seems to be right

I'm writing a very basic program in java but I get the error "error: cannot find symbol".

It's driving me crazy because I really don't know what I did wrong this time.

Here is the code of the main class

package glaces.tests;
import geometrie.Point ;
import glaces.Iceberg2D; 

public class TestIceberg2D {

    public static void main (String[] args){

        Iceberg2D i1 = new Iceberg2D(new Point(2,3), new Point(6,7));
        Iceberg2D i2 = new Iceberg2D(new Point(3,7), new Point(5,9));

        Iceberg2D i3 = new Iceberg2D(i1,i2);
        System.out.println(i3.toString());

}
}

Here is the class Iceberg2D :

package glaces;
import geometrie.Point;
import java.lang.Math;

/**
 * Un iceberg rectangulaire
 * @author Martine Gautier, Université de Lorraine
 */
public class Iceberg2D {

    private Point enBasAGauche ;
    private Point enHautADroite ;

    /**
     * Construction
     * @param g le coin en bas à gauche
     * @param d le coin en haut à droite
     * uniquement en coordonnées positives
     */
    public Iceberg2D(Point g, Point d) {
        this.enBasAGauche = g;
    this.enHautADroite = d;
    }

    /**
     * Construction par fusion de deux icebergs qui se touchent
     * @param i1 premier iceberg à fusionner
     * @param i2 deuxième iceberg à fusionner
     */
    public Iceberg2D(Iceberg2D i1, Iceberg2D i2) {
        
    this.enBasAGauche = new Point (Math.min(i1.coinEnBasAGauche().getAbscisse(),i2.coinEnBasAGauche().getAbscisse()),Math.min(i1.coinEnBasAGauche().getOrdonnee(),i2.coinEnBasAGauche().getOrdonnee()));
    

    this.enHautADroite = new Point (Math.max(i1.coinEnHautADroite().getAbscisse(),i2.coinEnHautADroite().getAbscisse()),Math.max(i1.coinEnHautADroite().getOrdonnee(),i2.coinEnHautADroite().getOrdonnee()));
}

    /**
     * Retourne le coin en bas à gauche
     * @return le coin en bas à gauche
     */
    public Point coinEnBasAGauche() {
        return this.enBasAGauche ;
    }

    /**
     * Retourne le coin en haut à droite
     * @return le coin en haut à droite
     */
    public Point coinEnHautADroite() {
        return this.enHautADroite ;
    }


    /**
     * Retourne la hauteur
     * @return hauteur
     */
    public double hauteur() {

        return Math.abs(enHautADroite.getOrdonnee()-enBasAGauche.getOrdonnee());
    }

    /**
     * Retourne la largeur
     * @return largeur
     */
    public double largeur() {
        return Math.abs(enHautADroite.getAbscisse()-enBasAGauche.getAbscisse());
    }

    /**
     * Retourne la surface totale
     * @return surface totale
     */
    public double surface() {
        return hauteur()*largeur() ;
    }

    /**
     * Retourne vrai si il y a une collision entre les deux icebergs
     * @param i iceberg potentiellement en collision
     * @return vrai si collision entre les deux icebergs
     */
    public boolean collision(Iceberg2D i) {

    

    if(this.enBasAGauche.getAbscisse() + largeur() == i.enBasAGauche.getAbscisse() || i.enBasAGauche.getAbscisse() + i.largeur() == this.enBasAGauche.getAbscisse()){
        
        if(this.enBasAGauche.getOrdonnee() + hauteur() >= i.coinEnBasAGauche().getOrdonnee() && i.enBasAGauche.getOrdonnee() + i.hauteur() >= this.coinEnBasAGauche().getOrdonnee()){
            return true;
        }}
     
    if(this.enBasAGauche.getOrdonnee() + hauteur() == i.enBasAGauche.getOrdonnee() || i.enBasAGauche.getOrdonnee() + i.hauteur() == this.enBasAGauche.getOrdonnee()){
        
        if(this.enBasAGauche.getAbscisse() + largeur() >= i.coinEnBasAGauche().getAbscisse() && i.enBasAGauche.getAbscisse() + i.largeur() >= this.coinEnBasAGauche().getAbscisse()){
            return true;
        }   
}           
        
    
        return false ;
    }

    /**
     * Retourne vrai si this est plus volumineux que i
     * @param i iceberg à comparer
     * @return vrai si this est plus volumineux que i
     */
    public boolean estPlusGrosQue(Iceberg2D i) {
    
        return this.surface() > i.surface();
    }

    public String toString() {
        return "Point bas gauche : "+enBasAGauche.toString()+" / Point haut droite : "+enHautADroite.toString() ;
    }

    /**
     * Retourne le point au centre
     * @return le point au centre de l'iceberg
     */
    public Point centre() {

    return new Point((enBasAGauche.getAbscisse()+enHautADroite.getAbscisse())/2,(enBasAGauche.getOrdonnee()         +enHautADroite.getOrdonnee())/2);
    }

    /**
     * Réduction dans les quatre directions ; le centre ne bouge pas
     * @param fr dans ]0..1[ facteur de réduction
     */
    public void fondre(double fr) {
        fr = fr/2;
    double hauteur = hauteur();
    double largeur = largeur();
    enBasAGauche.deplacer(largeur*fr,hauteur*fr);
    enHautADroite.deplacer(-largeur*fr,-hauteur*fr);
    }

    /**
     * Casser une partie à droite
     * @param fr dans ]0..1[ facteur de réduction
     */
    public void casserDroite(double fr) {
    fr = fr/2;
        enHautADroite.deplacer(-largeur()*fr,0);
    }

    /**
     * Casser une partie à gauche
     * @param fr dans ]0..1[ facteur de réduction
     */
    public void casserGauche(double fr) {
        fr = fr/2;
    enBasAGauche.deplacer(largeur()*fr,0); 
    }

    /**
     * Casser une partie en haut
     * @param fr dans ]0..1[ facteur de réduction
     */
    public void casserHaut(double fr) {
    fr = fr/2;
        enHautADroite.deplacer(0,-hauteur()*fr); 
    }

    /**
     * Casser une partie en bas
     * @param fr dans ]0..1[ : définit le pourcentage supprimé
     */
    public void casserBas(double fr) {
        fr = fr/2;
    enBasAGauche.deplacer(0,hauteur()*fr);
    }

}

here is the error I get :

javac -classpath ../ressourcesBPO/geometrie.jar -encoding "iso-8859-1" glaces/tests/TestIceberg2D.java
glaces/tests/TestIceberg2D.java:3: error: cannot find symbol
import glaces.Iceberg2D; 
             ^
  symbol:   class Iceberg2D
  location: package glaces
glaces/tests/TestIceberg2D.java:9: error: cannot find symbol
        Iceberg2D i1 = new Iceberg2D(new Point(2,3), new Point(6,7));
        ^
  symbol:   class Iceberg2D
  location: class TestIceberg2D
glaces/tests/TestIceberg2D.java:9: error: cannot find symbol
        Iceberg2D i1 = new Iceberg2D(new Point(2,3), new Point(6,7));
                           ^
  symbol:   class Iceberg2D
  location: class TestIceberg2D
glaces/tests/TestIceberg2D.java:10: error: cannot find symbol
        Iceberg2D i2 = new Iceberg2D(new Point(3,7), new Point(5,9));
        ^
  symbol:   class Iceberg2D
  location: class TestIceberg2D
glaces/tests/TestIceberg2D.java:10: error: cannot find symbol
        Iceberg2D i2 = new Iceberg2D(new Point(3,7), new Point(5,9));
                           ^
  symbol:   class Iceberg2D
  location: class TestIceberg2D
glaces/tests/TestIceberg2D.java:58: error: cannot find symbol
        Iceberg2D i3 = new Iceberg2D(i1,i2);
        ^
  symbol:   class Iceberg2D
  location: class TestIceberg2D
glaces/tests/TestIceberg2D.java:58: error: cannot find symbol
        Iceberg2D i3 = new Iceberg2D(i1,i2);
                           ^
  symbol:   class Iceberg2D
  location: class TestIceberg2D
7 errors

This is my folder tree view

thanks in advance !!!

Upvotes: 0

Views: 307

Answers (2)

Konrad Rudolph
Konrad Rudolph

Reputation: 545518

Since your test is using the Iceberg2D class, you will first need to compile that and then point javac to its location as part of the classpath, or compile both sources at the same time.

Based on your directory structure, the first would be (assuming you’re in the top-level java directory):

javac -cp ../ressourcesBPO/geometrie.jar -d java java/glaces/Iceberg2D.java
javac -cp ../ressourcesBPO/geometrie.jar:. -d java java/glaces/test/TestIceberg2D.java

The point is that you need to add the top-level path of your *.class files to the classpath (i.e. where Java would search for the compiled file glaces/Iceberg2d.class) — and that is the current directory (i.e. .).

However, that’s ending up mixing compiled and source files, and makes everything more complicated than necessary. A more conventional Java project structure would have this outline:

projectname/
╰── build/
    ╰── classes/
        ├── main/
        ╰── test/
├── lib/
│   ╰── geometrie.jar
╰── src/
    ├── main/
    │   ╰── java/
    │       ╰── glaces/
    │           ╰── Iceberg2D.java
    ╰── test/
        ╰── java/
            ╰── glaces/
                ╰── TestIceberg2D.java

This simplifies the build command somewhat, and prevents cluttering your source tree:

shopt extglob # requires Bash to run!

javac -d build/classes/main -cp lib/geometrie.jar src/main/java/**/*.java
javac -d build/classes/test -cp lib/geometrie.jar:build/classes/main src/test/java/**/*.java

Furthermore, this is also the directory structure used by modern Java build systems such as Gradle. Using the latter, you could create a minimal build configuration (using gradle init) and then run gradle test to build your entire main source tree, the test source tree, and then run the tests.

A final note, I know it’s extremely common to program “in French” at University in France (been there, done the same) but I strongly recommend consistently using English when writing code. Names matter for code comprehension and mixing different languages when working with other libraries makes everything confusing. It also means that only French-speaking people can read or use your code. This means asking questions (e.g. here on Stack Overflow) gets harder, but also that you can’t usefully distribute your code once you write something cool and want to share it.

Upvotes: 1

Strauss
Strauss

Reputation: 43

Ok so I found the solution but I have absolutely no idea why it works. Here is what I typed in the terminal :

javac -classpath ../ressourcesBPO/geometrie.jar:. -encoding "iso-8859-1" glaces/tests/TestIceberg2D.java

I added :. after the .jar

Why it worked ? I have absolutely no idea so, if someone wants to explain it would be much appreciated. Thanks a lot btw for trying to help !

Upvotes: 1

Related Questions