Windows CMD 无法调用另一个类。

huangapple go评论73阅读模式
英文:

Windows CMD cannot call on another class

问题

以下是翻译好的内容:

我正试图在Java中编译我的代码,但每次尝试时都会给我一个错误,显示它找不到另一个类的符号。例如:

package helloWorld;
public class FirstClass {

       public static void main(String[] args) {
              SecondClass secondClass = new SecondClass();
              secondClass.HelloWorld();
       }

}

我的另一个类将是:

package helloWorld;
public class SecondClass {

        public void HelloWorld() {
               System.out.println("Hello World");
        }
}

如果我尝试编译代码,它会指向SecondClass的声明,并显示错误:无法找到符号。这段代码在像Eclipse或NetBeans这样的IDE中可以正常运行。

英文:

I am trying to compile my code in Java but every time I try it gives me the error saying it cannot find the symbol of the other class. For example:

package helloWorld;
public class FirstClass {

       public static void main(String[] args) {
              SecondClass secondClass = new SecondClass();
              class.HelloWorld();
       }

}

And my other class will be:

package helloWorld;
public class SecondClass {

        public void HelloWorld() {
               System.out.println("Hello World");
        }
}

If I try to compile the code it will point at SecondClass declaration and say, error: cannot find symbol. The code would run fine in an IDE like Eclipse or NetBeans.

答案1

得分: 1

class是关键字之一,但您试图将其用作变量名

 SecondClass class = new SecondClass();
 class.HelloWorld();

请将变量名更改为与class不同的名称,然后重试。

您不能将关键字如int、for、class等用作变量名(或标识符),因为它们是Java编程语言语法的一部分。

我进行了以下更改,然后成功运行了

public class FirstClass {
public static void main(String[] args) {
    	   SecondClass class1 = new SecondClass();
    	   class1.HelloWorld();
       }
}

D:\workspace_europa\DatastructureAndAlgorithms\src目录

09/13/2020  10:06 AM    <DIR>          .
09/13/2020  10:06 AM    <DIR>          ..
09/13/2020  10:11 AM               330 FirstClass.class
09/13/2020  10:06 AM               168 FirstClass.java
09/13/2020  10:11 AM               408 SecondClass.class
09/13/2020  09:57 AM               131 SecondClass.java

D:\workspace_europa\DatastructureAndAlgorithms\src>d:\InstalledProgramms\Java\jdk1.5.0_22\bin\javac.exe -cp  . FirstClass.java

D:\workspace_europa\DatastructureAndAlgorithms\src>d:\InstalledProgramms\Java\jdk1.5.0_22\bin\javac.exe -cp  . FirstClass.java

D:\workspace_europa\DatastructureAndAlgorithms\src>d:\InstalledProgramms\Java\jdk1.5.0_22\bin\java.exe -cp  . FirstClass
Hello World

如果问题仍然存在,则在编译和运行时使用-verbose标志可以为您提供线索

D:\workspace_europa\DatastructureAndAlgorithms\src>d:\InstalledProgramms\Java\jdk1.5.0_22\bin\javac.exe -verbose -cp .  FirstClass.java

D:\workspace_europa\DatastructureAndAlgorithms\src>d:\InstalledProgramms\Java\jdk1.5.0_22\bin\java.exe -verbose -cp .  FirstClass
英文:

class is one of the keyword, but you are tyring to use as a variable name

 SecondClass class = new SecondClass();
 class.HelloWorld();

Please change the variable name to different name than class and retry.

You cannot use keywords like int, for, class, etc as variable name (or identifiers) as they are part of the Java programming language syntax.

I made following changes and the ran successfully

public class FirstClass {
public static void main(String[] args) {
    	   SecondClass class1 = new SecondClass();
    	   class1.HelloWorld();
       }
}

Directory of D:\workspace_europa\DatastructureAndAlgorithms\src

09/13/2020  10:06 AM    <DIR>          .
09/13/2020  10:06 AM    <DIR>          ..
09/13/2020  10:11 AM               330 FirstClass.class
09/13/2020  10:06 AM               168 FirstClass.java
09/13/2020  10:11 AM               408 SecondClass.class
09/13/2020  09:57 AM               131 SecondClass.java


D:\workspace_europa\DatastructureAndAlgorithms\src>d:\InstalledProgramms\Java\jdk1.5.0_22\bin\javac.exe -cp  . FirstClass.java

D:\workspace_europa\DatastructureAndAlgorithms\src>d:\InstalledProgramms\Java\jdk1.5.0_22\bin\javac.exe -cp  . FirstClass.java

D:\workspace_europa\DatastructureAndAlgorithms\src>d:\InstalledProgramms\Java\jdk1.5.0_22\bin\java.exe -cp  . FirstClass
Hello World

If you are still facing issue then using -verbose flag during compilation and runtime can give you the lead

D:\workspace_europa\DatastructureAndAlgorithms\src>d:\InstalledProgramms\Java\jdk1.5.0_22\bin\javac.exe -verbose -cp .  FirstClass.java

D:\workspace_europa\DatastructureAndAlgorithms\src>d:\InstalledProgramms\Java\jdk1.5.0_22\bin\java.exe -verbose -cp .  FirstClass

答案2

得分: 0

尝试从父文件夹运行它。例如,如果您的包保存在文件夹A中,则按以下方式运行程序 -

javac A/helloWorld/FirstClass.java
java A/helloWorld/FirstClass

这应该可以正常工作。

英文:

Try to run it from the parent folder. Like if your package is kept in the A folder then run the program like this -

javac A/helloWorld/FirstClass.java
java A/helloWorld/FirstClass

This should work.

huangapple
  • 本文由 发表于 2020年9月13日 12:12:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/63867077.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定