英文:
error : cannot find symbol with Javac shows up when everything seems to be right
问题
我正在使用Java编写一个非常基本的程序,但是我遇到了错误:"error: cannot find symbol"。
这真让我疯狂,因为我真的不知道这次我做错了什么。
这是主类的代码:
```java
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());
}
}
这是Iceberg2D类:
package glaces;
import geometrie.Point;
import java.lang.Math;
/**
* 一个矩形冰山
* 作者:Martine Gautier,Lorraine大学
*/
public class Iceberg2D {
private Point enBasAGauche;
private Point enHautADroite;
public Iceberg2D(Point g, Point d) {
this.enBasAGauche = g;
this.enHautADroite = d;
}
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()));
}
public Point coinEnBasAGauche() {
return this.enBasAGauche;
}
public Point coinEnHautADroite() {
return this.enHautADroite;
}
public double hauteur() {
return Math.abs(enHautADroite.getOrdonnee() - enBasAGauche.getOrdonnee());
}
public double largeur() {
return Math.abs(enHautADroite.getAbscisse() - enBasAGauche.getAbscisse());
}
public double surface() {
return hauteur() * largeur();
}
public boolean collision(Iceberg2D 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();
}
public Point centre() {
// 省略计算中心点的部分
}
public void fondre(double fr) {
// 省略融化的部分
}
public void casserDroite(double fr) {
// 省略向右破裂的部分
}
public void casserGauche(double fr) {
// 省略向左破裂的部分
}
public void casserHaut(double fr) {
// 省略向上破裂的部分
}
public void casserBas(double fr) {
// 省略向下破裂的部分
}
}
这是我收到的错误信息:
javac -classpath ../ressourcesBPO/geometrie.jar -encoding "iso-8859-1" glaces/tests/TestIceberg2D.java
glaces/tests/TestIceberg2D.java:3: 错误: 找不到符号
import glaces.Iceberg2D;
^
符号: 类 Iceberg2D
位置: 包 glaces
glaces/tests/TestIceberg2D.java:9: 错误: 找不到符号
Iceberg2D i1 = new Iceberg2D(new Point(2, 3), new Point(6, 7));
^
符号: 类 Iceberg2D
位置: 类 TestIceberg2D
glaces/tests/TestIceberg2D.java:9: 错误: 找不到符号
Iceberg2D i1 = new Iceberg2D(new Point(2, 3), new Point(6, 7));
^
符号: 类 Iceberg2D
位置: 类 TestIceberg2D
glaces/tests/TestIceberg2D.java:10: 错误: 找不到符号
Iceberg2D i2 = new Iceberg2D(new Point(3, 7), new Point(5, 9));
^
符号: 类 Iceberg2D
位置: 类 TestIceberg2D
glaces/tests/TestIceberg2D.java:10: 错误: 找不到符号
Iceberg2D i2 = new Iceberg2D(new Point(3, 7), new Point(5, 9));
^
符号: 类 Iceberg2D
位置: 类 TestIceberg2D
glaces/tests/TestIceberg2D.java:58: 错误: 找不到符号
Iceberg2D i3 = new Iceberg2D(i1, i2);
^
符号: 类 Iceberg2D
位置: 类 TestIceberg2D
glaces/tests/TestIceberg2D.java:58: 错误: 找不到符号
Iceberg2D i3 = new Iceberg2D(i1, i2);
^
符号: 类 Iceberg2D
位置: 类 TestIceberg2D
7 个错误
这是我的文件夹树视图:
感谢您的帮助!
<details>
<summary>英文:</summary>
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][1]
[1]: https://i.stack.imgur.com/cnK3A.png
thanks in advance !!!
</details>
# 答案1
**得分**: 1
好的,这是翻译好的部分:
好的,我找到了解决方案,但我完全不知道它为什么起作用。
以下是我在终端中输入的内容:
```javac -classpath ../ressourcesBPO/geometrie.jar:. -encoding "iso-8859-1" glaces/tests/TestIceberg2D.java```
我在`.jar`之后添加了`:.`。
为什么它起作用了?我真的一点都不清楚,所以如果有人想解释一下,我会非常感谢。顺便说一下,非常感谢您的帮助!
<details>
<summary>英文:</summary>
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 !
</details>
# 答案2
**得分**: 1
自从你的测试中使用了 `Iceberg2D` 类,你首先需要编译*那个*类,然后将 `javac` 定位到其位置作为类路径的一部分,或者同时编译两个源文件。
基于你的目录结构,首先是这样的(假设你在顶层 `java` 目录中):
```bash
javac -cp ../ressourcesBPO/geometrie.jar -d java java/glaces/Iceberg2D.java
javac -cp ../ressourcesBPO/geometrie.jar:. -d java java/glaces/test/TestIceberg2D.java
关键在于你需要将你的 *.class
文件的顶层路径添加到类路径中(即 Java 将搜索编译后文件 glaces/Iceberg2d.class
的位置)——这个位置就是当前目录(即 .
)。
然而,这会导致编译和源文件混在一起,使得一切比必要复杂。更常规的 Java 项目结构应该是这样的:
projectname/
╰── build/
╰── classes/
   ├── main/
   ╰── test/
├── lib/
│   ╰── geometrie.jar
╰── src/
├── main/
│   ╰── java/
│   ╰── glaces/
│   ╰── Iceberg2D.java
╰── test/
╰── java/
╰── glaces/
╰── TestIceberg2D.java
这会稍微简化构建命令,防止源树混乱:
shopt extglob # 需要 Bash 执行!
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
此外,这也是现代 Java 构建系统(如 Gradle)使用的目录结构。使用 Gradle,你可以创建一个最小的构建配置(使用 gradle init
),然后运行 gradle test
来构建整个主源树、测试源树,并运行测试。
最后注意一点,我知道在法国的大学里“用法语”编程非常普遍(我也有这样的经历),但我强烈建议在编写代码时始终使用英语。名称很重要,对于代码理解很重要,而在使用其他库时混合不同语言会让一切变得混乱。这也意味着只有说法语的人才能阅读或使用你的代码。这意味着一旦你写了一些很酷的东西并希望分享它,提问(例如在 Stack Overflow 上)变得更加困难,同时也意味着你不能有用地分发你的代码。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论