简体   繁体   English

使用Eclipse IDE可以完美地编译和运行Java程序,但是当我使用“ javac”时,会导致错误

[英]Using Eclipse IDE can compile and run the Java program perfectly but when I use 'javac' it is resulting to Error

I am new to Eclipse and java programming, so please be gentle and any help is highly appreciated. 我是Eclipse和Java编程的新手,所以请保持谨慎,任何帮助都将受到高度赞赏。

I recently wrote a program using java and Eclipse IDE. 我最近使用Java和Eclipse IDE编写了一个程序。

I made the following class: 我做了以下课程:

package Test;
import java.util.*;
import Test.AnotherClass;

public class Program{
  public static void main(String[] args){
    AnotherClass ac = new AnotherClass();
    ac.callingAMethod();
  }
}

This class resides in a file named Program.java and I made another file named AnotherClass.java which is implemented like this: 该类位于一个名为Program.java的文件中,我制作了另一个名为AnotherClass.java的文件,其实现方式如下:

package Test;
import java.util.*;

public class AnotherClass{
  public void callingAMethod(){
    System.out.println("Hello, World!");
  }
}

Now, if I use Eclipse then the program runs perfectly and even shows the output as "Hello, World!" 现在,如果我使用Eclipse,则程序可以完美运行,甚至将输出显示为“ Hello,World!”。 in the console of Eclipse. 在Eclipse的控制台中。 But if I use Terminal and javac to compile it gives me the following error: 但是,如果我使用Terminal和javac进行编译,则会出现以下错误:

Program.java:3: error: cannot find symbol import Test.AnotherClass; Program.java:3:错误:找不到符号导入Test.AnotherClass; ^ symbol: class AnotherClass location: package Test Program.java:58: error: cannot find symbol AnotherClass ac = new AnotherClass(); ^符号:类AnotherClass位置:软件包Test Program.java:58:错误:找不到符号AnotherClass ac = new AnotherClass(); ^ symbol: ^符号:
class AnotherClass location: class Program Program.java:58: error: cannot find symbol AnotherClass ac = new AnotherClass(); class AnotherClass位置:类Program Program.java:58:错误:找不到符号AnotherClass ac = new AnotherClass(); ^ symbol: class AnotherClass location: class Program 3 errors ^符号:类AnotherClass位置:类程序3错误

Another issue is Eclipse creates built in .class files in /bin and if i execute then on the Terminal then it gives me the following error: 另一个问题是Eclipse在/ bin中创建内置的.class文件,如果我在终端上执行,则出现以下错误:

Error: Could not find or load main class Program 错误:找不到或加载主类程序

I can not happen to find the issue, how come the program can compile in Eclipse IDE and show the output as well, whereas when I use 'javac' to compile and then use 'java' to run it is throwing errors. 我找不到问题所在,程序如何在Eclipse IDE中进行编译并显示输出,而当我使用“ javac”进行编译然后使用“ java”运行时,则会引发错误。

Any help is appreciated. 任何帮助表示赞赏。 Thank you. 谢谢。

You should first compile your classes AnotherClass.java and Program.java to create binary files(.class files) like this: 您应该首先编译您的类AnotherClass.java和Program.java以创建二进制文件(.class文件),如下所示:

javac Program.java AnotherClass.java 

Then you should go to the directory which the package name "test" exists. 然后,您应该转到包名称为“ test”的目录。 Then you should run the below command which also include the classpath while calling the class that contains your main method: 然后,应在调用包含主方法的类时运行以下命令,该命令还包括类路径:

java -classpath . test.Program

When you try to compile Program.java using javac, it's expecting the class file for AnotherClass, as you have referred it in this class, even though eclipse has created this class file when you ran the code javac is not aware of this class file, so solution for first error is 当您尝试使用javac编译Program.java时,期望使用的是ClassClass的类文件,就像您在此类中引用的那样,即使eclipse在运行代码时创建了该类文件,javac也不知道该类文件,所以第一个错误的解决方案是

  1. Compile AnotherClass.java using javac first then compile Program.java 首先使用javac编译AnotherClass.java,然后再编译Program.java

  2. The second issue is when you have included a class within package and compile using javac it wouldn't create package structure, to create package structure give below command 第二个问题是,当您在包中包含一个类并使用javac进行编译时,它不会创建包结构,要创建包结构,请使用以下命令

    javac -d . javac -d。 AnotherClass.java AnotherClass.java

    javac -d . javac -d。 Program.java 程式库

    java Test.Program java Test.Program

you can replace "." 您可以替换为“。” with any directory, I gave "." 与任何目录,我给“。” to create package structure within the same directory. 在同一目录中创建包结构。 This gives you required output 这为您提供了所需的输出

To use command line to compile, you should specify some parameters other than the class filename in order to compile more than 1 file in one go: 要使用命令行进行编译,您应该指定除类文件名以外的一些参数,以便一次编译多个文件:

javac -sourcepath src -d bin src\Test\Program.java

It will find and compile all your related classes together with Program.java , ie, AnotherClass.java , located in the sourcepath src , and place all the compiled classes in bin , like what eclipse does. 它将与AnotherClass.java src中的Program.java (即AnotherClass.java一起找到并编译所有相关类,并将所有已编译的类放入bin ,就像eclipse所做的那样。

To execute your program located under bin : 要执行bin下的程序:

java -classpath bin Test.Program

It will find the compiled classes in bin and execute it. 它将在bin找到已编译的类并执行它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试使用javac编译.java helloworld程序时出错 - Error when trying to compile .java helloworld program using javac 当我使用`javac`进行编译时,如何解决此类问题:“错误:找不到符号[...]”? - How can I solve that type of issue when I compile using `javac`: " error: cannot find symbol [...]"? Apache POI Java程序在IDE(Eclipse)中运行良好,但是当我尝试将其作为可运行的jar运行时遇到NoClassDefFoundError - Apache POI Java program working fine in IDE (Eclipse) but encounter NoClassDefFoundError when I tried to run it as a runnable jar noclassdeffounderror 在一个简单的网络程序中。 我正在使用 Eclipse IDE 运行 java 程序 - noclassdeffounderror in a simple networking program. I am using Eclipse IDE to run java programs Java泛型在Eclipse中编译,但不在javac中编译 - Java generics compile in Eclipse, but not in javac 如何编译和运行Java程序? - How can I compile and run a Java program? 以 Java 应用程序运行程序时,Eclipse IDE 中的“启动错误” - 'Launch Error' in Eclipse IDE when running program as Java Application 如何使用javac和java为asterisk-java编译和运行ExampleCallIn - how to compile and run ExampleCallIn for asterisk-java using javac and java Eclipse IDE编译错误 - Eclipse IDE compile error javac 11可以编译将在java 8 JVM上运行的二进制文件/ jar吗? - Can javac 11 compile binaries/jars that will run on a java 8 JVM?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM