我遇到了一些问题,程序找不到所需的类,尽管它们具有相同的目录和相同的包声明。
Eclipse 说“无法找到或运行主类”,所以我尝试手动编译它。编译器是这样说的:
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. Alle Rechte vorbehalten.
C:\Users\Gilbert>cd C:\Users\Gilbert\Desktop\factory
C:\Users\Gilbert\Desktop\factory>"C:\Program Files\Java\jdk1.7.0_45\bin\javac" P
rodukt.java
C:\Users\Gilbert\Desktop\factory>"C:\Program Files\Java\jdk1.7.0_45\bin\javac" S
tation.java
C:\Users\Gilbert\Desktop\factory>"C:\Program Files\Java\jdk1.7.0_45\bin\javac" L
ine.java
Line.java:8: error: cannot find symbol
private Produkt produkt;
^
symbol: class Produkt
location: class Line
Line.java:9: error: cannot find symbol
private ArrayList<Station> stations;
^
symbol: class Station
location: class Line
Line.java:12: error: cannot find symbol
public Line(String name, Produkt produkt){
^
symbol: class Produkt
location: class Line
Line.java:17: error: cannot find symbol
public void add(Station station){
^
symbol: class Station
location: class Line
Line.java:25: error: cannot find symbol
public Station getStation(int number){
^
symbol: class Station
location: class Line
5 errors
这是代码(“尝试编译”类的代码,不包括主类):
文件1:
package factory;
public class Produkt{
private String name;
public Produkt(String name){
this.name = name;
}
public String getName(){
return this.name;
}
public void setName(String name){
this.name = name;
}
}
文件2:
package factory;
public class Station{
private int number;
private int time;
public Station(int number, int time){
this.number = number;
this.time = time;
}
public int getNumber(){
return this.number;
}
public void setNumber(int number){
this.number = number;
}
public int getTime(){
return this.time;
}
public void setTime(int time){
this.time = time;
}
}
文件3:
package factory;
import java.util.ArrayList;
public class Line{
private String name;
private Produkt produkt;
private ArrayList<Station> stations;
private int time;
public Line(String name, Produkt produkt){
this.name = name;
this.produkt = produkt;
}
public void add(Station station){
stations.add(station.getNumber(), station);
}
public String getName(){
return this.name;
}
public Station getStation(int number){
return stations.get(number);
}
public String getProduktName(){
return produkt.getName();
}
public int getTime(){
for (int i = 0; i < stations.size(); i++){
this.time = this.time + stations.get(i).getTime();
}
return this.time;
}
public int getStations(){
return stations.size();
}
}
完整程序的下载链接:https://dl.dropboxusercontent.com/u/34761768/factory%20-%20stack%20overflow.zip
编辑:
好的,这是主类:
封装工厂;
公共(public)类主要{
public static void main(String[] args){
Factory factory = new Factory("");
factory.reader(args[0]);
factory.print();
System.out.println("----");
factory.printBestWayOf2();
System.out.println("----");
factory.printBestWay();
}
}
它使用了我没有展示的另一个类,因为它是其中最长的。 我没有显示主类,因为错误发生在我尝试编译主类或其他类时。我只能编译那些对包中其他类没有任何引用的类,例如 produkt 和 station,但任何其他类都找不到它们,尽管它们都是包的一部分并且都在目录中。
最佳答案
您需要一个带有
main
方法的类。单独编译时会出现所有这些错误,因为这些类相互依赖。您需要使用
javac *.java
同时编译所有内容
执行 2 后的事件,您的程序将无法运行,因为 1。
如果您不知道,这是一个 main
方法
public static void main(String[] args) {
}
创建另一个类来运行应用程序。像这样的事情
public class App {
public static void main(String[] args) {
Produkt p = new Produkt("product");
Line line = new Line("line", p);
line.add(new Station(1, 2));
....
}
}
<小时/>
其他注释。
不确定您是否需要这个
public void add(Station station){ stations.add(station.getNumber(), station); }
您使用的
add
方法,第一个参数是ArrayList
的索引。这会给你带来问题,索引可能不存在add(int index, E element) Inserts the specified element at the specified position in this list.
就这么做
stations.add(station);
您应该在构造函数中初始化
的愤怒stations
。如果你不这样做,你将面临NullPointerException
public Line(String name, Produkt produkt){ this.name = name; this.produkt = produkt; station = new ArrayList<Station>(); }
如果您想稍后打印对象,您可能需要重写类中的
toString
。
更新其余代码
不要不要不要将字符串与
==
进行比较。使用等于
if (parts[0] == "factory")
更改为
if ("factory".equals(parts[0]))
更改全部。
我在编译您的代码时遇到问题。
您可能还需要设置启动配置。您需要右键单击您的项目,然后转到属性。单击运行/调试设置。如果您的
Main
类不在该列表中,请单击“新建”,然后单击“Java 应用程序”,然后找到您的类并确保它是主类。确保使用完全限定名称,例如mypackage.Main
关于java - 找不到类(class),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21498898/