[英]Error: Can't find or load main class when using Java command after compiling a .Java file with multiple classes
I try to run the java command after compiling my .java file, the .java file contains multiple classes but only one public class and only one main method in the public class.我在编译我的 .java 文件后尝试运行 java 命令,该 .java 文件包含多个类但只有一个公共类和公共类中只有一个主要方法。 It can't find the main class when all the .class files are in the same place.
当所有 .class 文件都在同一个地方时,它找不到主类。 On netbeans it runs perfectly, on CMD is where the error appears.
在 netbeans 上它运行完美,在 CMD 上是出现错误的地方。 Also sorry if I make any mistakes in my writing, English isn't my main language, it is Spanish.
如果我在写作中犯了任何错误,也很抱歉,英语不是我的主要语言,它是西班牙语。 Thats also why the classes and variables names are in Spanish.
这也是为什么类和变量名称是西班牙语的原因。
I already checked that the class only have one main method, all the classes are on the same place and there is only one public class in the .java file我已经检查过这个类只有一个 main 方法,所有的类都在同一个地方,.java 文件中只有一个公共类
That is the code and as you can see only one public class and main is in public class.这就是代码,您可以看到只有一个公共类,而 main 位于公共类中。
package dfutreras_arqapp_aa19_2b;
public class Dfutreras_ArqApp_AA19_2B {
public static void main(String[] args) {
// TODO code application logic here
Constructora constructora = new Constructora();
ConstruyeCasas constructor_SencillaCasa = new ConstruyeCasasSencillas();
ConstruyeCasas constructor_FamiliarCasa = new ConstruyeCasasFamiliares();
constructora.setConstruyeCasas(constructor_SencillaCasa);
constructora.construirCasa();
Casa casa = constructora.getCasa();
System.out.println("Se ha construido una casa " + casa.getTipo());
System.out.println("Con " + casa.getHabitaciones()+ " habitaciones");
System.out.println("Con " + casa.getBanos()+ " baños");
System.out.println("Con " + casa.getVentanas()+ " ventanas");
System.out.println();
constructora.setConstruyeCasas(constructor_FamiliarCasa);
constructora.construirCasa();
Casa casa2 = constructora.getCasa();
System.out.println("Se ha construido una casa " + casa2.getTipo());
System.out.println("Con " + casa2.getHabitaciones()+ " habitaciones");
System.out.println("Con " + casa2.getBanos()+ " baños");
System.out.println("Con " + casa2.getVentanas()+ " ventanas");
}
}
//Clase producto //类产品
`class Casa{`
private Integer habitaciones;
private Integer banos;
private Integer ventanas;
private String tipo;
public void setHabitaciones(Integer habitaciones){
this.habitaciones = habitaciones;
}
public void setBanos(Integer banos){
this.banos = banos;
}
public void setVentanas(Integer ventanas){
this.ventanas = ventanas;
}
public void setTipo(String tipo){
this.tipo = tipo;
}
public Integer getHabitaciones(){
return habitaciones;
}
public Integer getBanos(){
return banos;
}
public Integer getVentanas(){
return ventanas;
}
public String getTipo(){
return tipo;
}
}
//Clase Abstract Builder //类抽象生成器
abstract class ConstruyeCasas{
protected Casa casa;`
public Casa getCasa(){
return casa;
}
public void crearNuevaCasa(){
casa = new Casa();
}
public abstract void constuirHabitaciones();
public abstract void construirBanos();
public abstract void construirVentanas();
public abstract void asignarTipo();
}
//Clase construye casas sencillas //类解释casas sencillas
class ConstruyeCasasSencillas extends ConstruyeCasas{
@Override
public void constuirHabitaciones(){
casa.setHabitaciones(1);
}
@Override
public void construirBanos(){
casa.setBanos(2);
}
@Override
public void construirVentanas(){
casa.setVentanas(5);
}
@Override
public void asignarTipo(){
casa.setTipo("Sencilla");
}
}
`//Clase construye casas familiares
`
class ConstruyeCasasFamiliares extends ConstruyeCasas{
@Override
public void constuirHabitaciones(){
casa.setHabitaciones(3);
}
@Override
public void construirBanos(){
casa.setBanos(4);
}
@Override
public void construirVentanas(){
casa.setVentanas(8);
}
@Override
public void asignarTipo(){
casa.setTipo("Familiar");
}
}
//Clase directora //类导演
class Constructora{
private ConstruyeCasas construyeCasas;
public void setConstruyeCasas(ConstruyeCasas consCasas){
construyeCasas = consCasas;
}
public Casa getCasa(){
return construyeCasas.getCasa();
}
public void construirCasa(){
construyeCasas.crearNuevaCasa();
construyeCasas.constuirHabitaciones();
construyeCasas.construirBanos();
construyeCasas.construirVentanas();
construyeCasas.asignarTipo();
}
}
The directory of the .java and the .class files are this: .java 和 .class 文件的目录是这样的:
C:\Users\hecto_000\Documents\NetBeansProjects\Dfutreras_ArqApp_AA19_2B\src\dfutreras_arqapp_aa19_2b
I expect that when I run java Dfutreras_ArqApp_AA19_2B in the CMD after running javac Dfutreras_ArqApp_AA19_2B.java will execute the program and doesn't give me the error: Error Could not find or load the main class Dfutreras_ArqApp_AA19_2B.
我希望当我在运行 javac Dfutreras_ArqApp_AA19_2B.java 后在 CMD 中运行 java Dfutreras_ArqApp_AA19_2B 时,将执行程序并且不会给我错误:
Error Could not find or load the main class Dfutreras_ArqApp_AA19_2B.
Your class is declared as :你的班级被声明为:
package dfutreras_arqapp_aa19_2b;
public class Dfutreras_ArqApp_AA19_2B {
This means it is in the package dfutreras_arqapp_aa19_2b
, and so : - when run the whole package name must be supplied, and - The current directory must be at the root (top-level) directory.这意味着它在包
dfutreras_arqapp_aa19_2b
,因此: - 运行时必须提供整个包名称,并且 - 当前目录必须位于根(顶级)目录中。
Putting these together means that to run the class, go to the src directory :将这些放在一起意味着要运行该类,请转到 src 目录:
C:\Users\hecto_000\Documents\NetBeansProjects\Dfutreras_ArqApp_AA19_2B\src
And run the command:并运行命令:
java dfutreras_arqapp_aa19_2b.Dfutreras_ArqApp_AA19_2B
To run the class from the current directory, remove the line package dfutreras_arqapp_aa19_2b
from the .java file(s), which will mean the class is in the "default" package.要从当前目录运行该类,
package dfutreras_arqapp_aa19_2b
从 .java 文件中删除行package dfutreras_arqapp_aa19_2b
,这意味着该类位于“默认”包中。
Coincidentally, it is also worth mentioning that Java naming standards are that class names should be "CamelCase" (so without underscores)巧合的是,还值得一提的是,Java命名标准是类名应该是“CamelCase”(所以没有下划线)
在您的所有类中包含 (package dfutreras_arqapp_aa19_2b;) 包,然后尝试一下。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.