简体   繁体   English

Eclipse 不会编译/运行 java 文件

[英]Eclipse won't compile/run java file

I am just trying to compile and run a simple java program.我只是想编译和运行一个简单的 java 程序。 When I go to run my tester class it says select what to run and it gives me Ant Build which when highlighted says "Launches an Ant build with default settings" or Ant Build... that says "Launches an Ant build and allows it to be configured". When I go to run my tester class it says select what to run and it gives me Ant Build which when highlighted says "Launches an Ant build with default settings" or Ant Build... that says "Launches an Ant build and allows it to进行配置”。 When I try to select either of these it prompts Build failed.当我尝试 select 时,它会提示构建失败。 Reason: Unable to find ant file to run.原因:找不到 ant 文件运行。 I honestly don't know what these ant builds and files are.老实说,我不知道这些 ant 构建和文件是什么。 This is definitely a dumb question but have no idea what to do.这绝对是一个愚蠢的问题,但不知道该怎么做。

在此处输入图像描述

  • Make a project to put the files in. 制作一个项目来放入文件。
    • File -> New -> Java Project 文件 - >新建 - > Java项目
    • Make note of where that project was created (where your "workspace" is) 记下项目的创建位置(“工作区”所在的位置)
  • Move your java files into the src folder which is immediately inside the project's folder. 将您的java文件移动到项目文件夹中src文件夹中。
    • Find the project INSIDE Eclipse's Package Explorer (Window -> Show View -> Package Explorer) 找到项目INSIDE Eclipse的Package Explorer(Window - > Show View - > Package Explorer)
    • Double-click on the project, then double-click on the 'src' folder, and finally double-click on one of the java files inside the 'src' folder (they should look familiar!) 双击项目,然后双击'src'文件夹,最后双击'src'文件夹中的一个java文件(它们应该看起来很熟悉!)
  • Now you can run the files as expected. 现在您可以按预期运行文件。

Note the hollow 'J' in the image. 注意图像中的空心“J”。 That indicates that the file is not part of a project. 这表明该文件不是项目的一部分。

空心J意味着它不是项目的一部分

I was also in the same problem, check your build path in eclipse by Right Click on Project > build path > configure build path 我也遇到了同样的问题,通过右键单击Project > build path > configure build path来检查eclipse中的构建路径

Now check for Excluded Files, it should not have your file specified there by any means or by regex. 现在检查排除的文件,它不应该通过任何方式或正则表达式指定您的文件。

Cheers! 干杯! 错误修复视图的图像,请参阅排除字段

右键单击文件或项目浏览器中的某个位置,然后选择“运行方式” - >“java应用程序”

Your project has to have a builder set for it. 您的项目必须为其设置构建器。 If there is not one Eclipse will default to Ant. 如果没有一个Eclipse将默认为Ant。 Which you can use you have to create an Ant build file, which you can Google how to do. 你可以使用哪个来创建一个Ant构建文件,你可以在Google上做怎么做。 It is rather involved though. 但它相当复杂。 This is not required to run locally in Eclipse though. 这不是在Eclipse中本地运行所必需的。 If your class is run-able. 如果你的课程是可以运行的。 It looks like yours is, but we can not see all of it. 它看起来像你的,但我们看不到它。

If you look at your project build path do you have an output folder selected? 如果查看项目构建路径,是否选择了输出文件夹? If you check that folder have the compiled class files been put there? 如果检查该文件夹是否已将编译后的类文件放在那里? If not the something is not set in Eclpise for it to know to compile. 如果没有,Eclpise中没有设置某些东西让它知道要编译。 You might check to see if auto build is set for your project. 您可以检查是否为项目设置了自动构建。

This worked for me: 这对我有用:

  1. Create a new project 创建一个新项目
  2. Create a class in it 在其中创建一个类
  3. Add erroneous code, let error come 添加错误代码,让错误来临
  4. Now go to your project 现在去你的项目
  5. Go to Problems window 转到问题窗口
  6. Double click on a error 双击错误

It starts showing compilation errors in the code. 它开始在代码中显示编译错误。

What I did was I created a new project and opened the file location from my old project and new one.我所做的是创建了一个新项目,并从旧项目和新项目中打开了文件位置。 Then I copied everything from the old project besides the run file, and ran it on the new project and now it works fine.然后我从旧项目中复制除了运行文件之外的所有内容,然后在新项目上运行它,现在它工作正常。

the file is probably located in your project package but is not yet compiled so you are able to open it as a java file in your eclipse java project, here is what I did in my case: in eclipse, goto terminal window, cd to your project directory, then cd to your project package, then run javac yourFileName.java, finally right click your java project or project package in the project explorer and... you should see the file there, and it should now run for you as a java file... hope this helps!!! the file is probably located in your project package but is not yet compiled so you are able to open it as a java file in your eclipse java project, here is what I did in my case: in eclipse, goto terminal window, cd to your project directory, then cd to your project package, then run javac yourFileName.java, finally right click your java project or project package in the project explorer and... you should see the file there, and it should now run for you as a java 文件...希望这有帮助!!!

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM