java.lang.RuntimeException:无法编译的源代码 - 是什么导致了这种情况?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2333285/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 06:16:23  来源:igfitidea点击:

java.lang.RuntimeException: Uncompilable source code - what can cause this?

javanetbeans

提问by MalcomTucker

This error is just bizarre, my code compiles fine, I can see there are no problems with it, yet this error has just popped up. I have tried re-starting NetBeans and there is no additional exception information.

这个错误很奇怪,我的代码编译得很好,我可以看到它没有问题,但这个错误刚刚弹出。我已尝试重新启动 NetBeans,但没有其他异常信息。

What can cause this?

什么会导致这种情况?

采纳答案by Chandra Sekar

I guess you are using an IDE (like Netbeans) which allows you to run the code even if certain classes are not compilable. During the application's runtime, if you access this class it would lead to this exception.

我猜您正在使用 IDE(如 Netbeans),即使某些类不可编译,它也允许您运行代码。在应用程序运行期间,如果您访问此类,则会导致此异常。

回答by Dime

If it is Netbeans, try to uncheck "Compile on save" setting in the project properties (Build -> Compiling). This is the only thing which helped me in a similar situation.

如果是 Netbeans,请尝试在项目属性(构建 -> 编译)中取消选中“保存时编译”设置。这是在类似情况下唯一帮助我的事情。

Source: http://forums.netbeans.org/post-45324.html

来源:http: //forums.netbeans.org/post-45324.html

回答by mikato

Disable Deploy on Save in the Project's Properties/Run screen. That's what worked for me finally. Why the hell NetBeans screws this up is beyond me.

在项目的属性/运行屏幕中禁用保存时部署。这就是最终对我有用的东西。为什么 NetBeans 搞砸了我无法理解。

Note: I was able to compile the file it was complaining about using right-click in NetBeans. Apparently it wasn't really compiling it when I used Build & Compile since that gave no errors at all. But then after that, the errors just moved to another java class file. I couldn't compile then since it was grayed out. I also tried deleting the build and dist directories in my NetBeans project files but that didn't help either.

注意:我能够编译它抱怨在 NetBeans 中使用右键单击的文件。显然,当我使用 Build & Compile 时它并没有真正编译它,因为它根本没有给出任何错误。但在那之后,错误只是转移到另一个 java 类文件。那时我无法编译,因为它变灰了。我还尝试删除 NetBeans 项目文件中的 build 和 dist 目录,但这也无济于事。

回答by kommradHomer

It's caused by NetBeans retaining some of the old source and/or compiled code in its cache and not noticing that e.g. some of the code's dependencies (i.e. referenced packages) have changed, and that a proper refresh/recompile of the file would be in order.

这是由于 NetBeans 在其缓存中保留了一些旧的源代码和/或编译的代码,并且没有注意到例如某些代码的依赖项(即引用的包)已更改,并且文件的正确刷新/重新编译将是有序的.

The solution is to force that refresh by either:

解决方案是通过以下任一方式强制刷新:

a) locating & editing the offending source file to force its recompilation (e.g. add a dummy line, save, remove it, save again),
b) doing a clean build (sometimes will work, sometimes won't),
c) disabling "Compile on save" (not recommended, since it can make using the IDE a royal PITA), or
d) simply remove NetBeans cache by hand, forcing the recompilation.

a) 定位和编辑有问题的源文件以强制其重新编译(例如添加一个虚拟行,保存,删除它,再次保存),
b)进行干净的构建(有时会起作用,有时不会),
c)禁用“保存时编译”(不推荐,因为它可以使使用 IDE 成为皇家 PITA),或
d) 只需手动删除 NetBeans 缓存,强制重新编译。

As to how to remove the cache:

至于如何删除缓存:

If you're using an old version of NetBeans:

如果您使用的是旧版本的 NetBeans:

  • delete everything related to your project in .netbeans/6.9/var/cache/index/(replace 6.9 with your version).
  • 删除与您的项目相关的所有内容.netbeans/6.9/var/cache/index/(用您的版本替换 6.9)。

If you're using a newer one:

如果您使用的是较新的:

  • delete everything related to your project in AppData/Local/NetBeans/Cache/8.1/index/(replace 8.1 with your version).
  • 删除与您的项目相关的所有内容AppData/Local/NetBeans/Cache/8.1/index/(用您的版本替换 8.1)。

The paths may vary a little e.g. on different platforms, but the idea is still the same.

路径可能会有所不同,例如在不同的平台上,但想法仍然相同。

回答by UVM

I also got the same error and I did clean build and it worked.

我也遇到了同样的错误,我做了干净的构建并且它起作用了。

回答by Enrico Giurin

Organize your code as a maven module. Once done run the command from terminal
$mvn installl
to check if your code builds fine.
Finally import the project in netbeans or eclipse as maven project.

将您的代码组织为一个 Maven 模块。完成后,从终端运行命令
$mvn installl
以检查您的代码是否构建良好。
最后在 netbeans 或 eclipse 中将项目导入为 maven 项目。

回答by Kleyton

change the package of classes, your files are probably in the wrong package, happened to me when I copied the code from a friend, it was the default package and mine was another, hence the netbeans could not compile because of it.

更改类的包,你的文件可能在错误的包中,当我从朋友那里复制代码时发生在我身上,它是默认包,我的是另一个包,因此netbeans因此无法编译。

回答by Rogelio

I had this problem with NetBeans 8.0.1. Messages about problem in project deleted class. Deleting the ~/.netbeans didn't work. Also I looked for ANY reference to the deleted class in ALL my projects, nothing found. I deleted the build classes, everything. Then, when I started Netbeans again, compile and magically appears the message in Run and into the mother compiled class. I tried the uncheck "Compile on save" Dime solution, and works, but it's not practical. Finally, my solution was edit and force recompile of the mother class. This way the new .class doesn't contains the message and Run works OK.

我在 NetBeans 8.0.1 上遇到了这个问题。关于项目删除类中的问题的消息。删除 ~/.netbeans 不起作用。此外,我在所有项目中查找了对已删除类的任何引用,但一无所获。我删除了构建类,一切。然后,当我再次启动 Netbeans 时,编译并神奇地在 Run 中出现该消息并进入母编译类。我尝试取消选中“保存时编译”Dime 解决方案,并且有效,但不切实际。最后,我的解决方案是编辑并强制重新编译母类。这样新的 .class 不包含消息并且运行正常。

回答by Damir Olejar

Just check the packaging, the simplest answer I can provide is that your package has been mislabeled (within a class).

只需检查包装,我能提供的最简单的答案是您的包装贴错了标签(在一个班级内)。

Also, you may have some weird characters. Try white-flushing the code in a Notepad (or Gedit) and then pasting it into a newly created class with your IDE.

此外,您可能有一些奇怪的字符。尝试在记事本(或 Gedit)中刷新代码,然后将其粘贴到使用 IDE 新创建的类中。

回答by Ashtez

If you are using Netbeans, try to hit the Clean and Build button, let it do the thing and try again. Worked for me!

如果您使用的是 Netbeans,请尝试点击 Clean and Build 按钮,让它执行此操作,然后再试一次。为我工作!