60

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.

What can cause this?

MalcomTucker
  • 7,407
  • 14
  • 72
  • 93
  • Read the stack trace, it tells you *where* the problem occurs. – Joachim Sauer Feb 08 '11 at 09:38
  • 3
    That's not the issue. I get this about once a month and have to open each file in the project, add a space or a blank line and then save. It's something to do with your cached user files in $USER/.netbeans/var/cache/index/s*/java/*/classes – davidahines May 30 '11 at 19:25
  • 1
    note: some of the info in the answers here is either misleading, outdated or incomplete. I've taken the liberty to make a canonical answer from http://stackoverflow.com/a/10027643/719662 - it was IMO the one closest to being accurate in the historical sense. –  May 08 '16 at 15:38
  • Clean netbeans cache and try – vels4j Jun 08 '17 at 18:53

17 Answers17

78

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.

Dime
  • 2,041
  • 3
  • 23
  • 29
  • 1
    It works, but I wonder why if I do a Clean and Build when "Compile on save" is checked it doesn't regenerate the affected classes... – Oscar Pérez Nov 15 '13 at 19:51
  • I add none to settings.xml to disable "compile on save" for all projects by default. none – Harun Dec 09 '15 at 13:23
  • @vaxquis, I do not see a proper solution in kommradHomer's answer either. Just additional work arounds (e.g. add a dummy line, save, remove it, save again) to this one. IMHO it is a bug in Netbens. And IMHO the solution with unchecking "Compile on save" and then checking it again back is the simplest one. Thanks god this problem does not happen too often. – Dime May 09 '16 at 10:20
  • @vaxquis, I've provided the link that explains everything. You wrote that my answer is a workaround, but the other is the solution. My statement that both answers describe workarounds. The answer added a year go later describes more workarounds, which is fine and is very common on SO. Btw: If you need to automate a work-around, you do something wrong. You need to fix your project setup or change the IDE. – Dime May 10 '16 at 08:36
  • 1
    @Dime for one, as extensively covered on meta, [answers given in links are considered poor practice](http://meta.stackexchange.com/questions/8231/are-answers-that-just-contain-links-elsewhere-really-good-answers) and not an answer on SO, mainly due to a) link rot, b) poor readability. As soon as your link rots, your "answer" loses most of its value. The rule of thumb is to *describe roughly what the exact link provides*, giving *as much details as needed for the answer to function **without** the link*. I'm not into arguing with you, but SO policies are usually quite sensible. –  May 10 '16 at 12:54
  • @Dime and, secondly, *yes, you need to fix your project setup* - having an automated solution compensating for a bug is often considered a bugfix in situations when the bug is placed in third-party code. I agree there's a thin line between "workaround" and "bugfix" - yet, since this problem is actually more of a [heisenbug](https://en.wikipedia.org/wiki/Heisenbug), an automated issue detector & fixer is commonly implemented in such cases in real production environments. Also, thirdly, the link provided by you hardly explains anything and is barely related to the real issue here. –  May 10 '16 at 12:55
  • Link is broken. Useless answer – Alfabravo Sep 22 '20 at 02:00
  • @Alfabravo, the link is removed. This answer has helped more than 70 people. If it didn't help in your case, it does not mean it is useless. Please respect other people. Please post here a better answer if you know one. – Dime Sep 22 '20 at 11:59
  • Part of the rules of SO is to avoid link-only answers because this happens. It's not about you, your personal qualities or your will to help; the answer by itself was rendered useless because it was based on a link that is gone. It was told to you in a comment on 2016, it is still relevant. Can't see any source of disrespect in this. – Alfabravo Sep 22 '20 at 12:42
  • @Alfabravo, as I was also explaining above, the link was just the source of the information. It gave you no more information compared to my answer. The answer without a link is perfectly valid. It is not link-only answer, as you wrote. You may now start arguing that this is a workaround, etc. (as the guy before you), but, again, just feel free to provide a better one. No need to explain here how bad is the answer with 70 upvotes... – Dime Sep 24 '20 at 09:56
35

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.

Chandra Sekar
  • 10,683
  • 3
  • 39
  • 54
  • I have been trying to figure this error out for the past hour... thank you Chandru like a sir. In regards to your explaination does this mean if I'm editing the class in question during runtime this error will occur. – classicjonesynz May 17 '12 at 07:41
  • 2
    Actually, this answer together with Dime's answer is the correct answer. I had this issue in Netbeans, luckily I found the answer here. The real reason behind the issue is explained my your answer, how to solve it has been answered by Dime. I'll upvote both of your answers. – Timmos Jan 31 '13 at 20:13
  • 2
    frankly, it's hardly an answer. It's neither too accurate in describing the "why", and it completely ignores the "how to fix" part. –  May 08 '16 at 15:43
31

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.

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.

As to how to remove the cache:

If you're using an old version of NetBeans:

  • delete everything related to your project in .netbeans/6.9/var/cache/index/ (replace 6.9 with your version).

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).

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

kommradHomer
  • 4,127
  • 5
  • 51
  • 68
  • 2
    thank you, this was exactly the solution for my problem (works with jmonkey engine too).. in windows, go to %appdata% and locate .netbeans or .jmonkeyplatform :) – lunatix Jul 12 '13 at 22:40
  • 1
    This folder doesn't seem to exist anymore in 8.0.2. – Eusebius Aug 25 '15 at 13:05
  • 1
    @Eusebius I've edited the answer to include the new paths. –  May 08 '16 at 15:43
  • 1
    in windows 7 with netbeans 8.1 C:\Users\Servicios\AppData\Roaming\NetBeans\8.1 , but didnt find it – diego matos - keke Jun 07 '16 at 22:54
  • 1
    @keke In my case, within C:\Users\YourUsername\AppData there was a NetBeans folder both within the Roaming and the Local folders. In my case, there was no cache folder in the Roaming\NetBeans folder, only in the Local\NetBeans folder. – Candamir Jan 20 '18 at 05:16
  • In my case, only deleting the cache worked. Moreover, after deleting the cache, when trying to run the project, I got an error that said "could not find or load main class packageABC.Main". The remedy that [this answer](https://stackoverflow.com/a/21030299/8031521) talks about didn't work; ultimately only re-compiling the project and re-starting the IDE a few times did the trick. I'm sharing this for the benefit of others who might have the same problem. – Candamir Jan 20 '18 at 05:28
7

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

UVM
  • 9,776
  • 6
  • 41
  • 66
2

Add selenium-server-standalone-3.4.0.jar. It works to me. Download Link

2

Recheck the package declarations in all your classes!

This behaviour has been observed in NetBeans, when the package declaration in one of the classes of the package refers to a non-existent or wrong package. NetBeans normally detects and highlights this error but has been known to fail and misleadingly report the package as free of errors when this is not the case.

IqbalHamid
  • 2,324
  • 1
  • 18
  • 24
2

I had the same issue with one of my netbeans project.

Check whether you have correctly put the package name on all the classes. I got the same error message because i forgot to put the package name of a certain class (which was copied from another project).

chandima
  • 121
  • 4
1

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.

Enrico Giurin
  • 2,183
  • 32
  • 30
1

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.

Kleyton
  • 21
  • 1
1

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.

Rogelio Triviño
  • 5,961
  • 3
  • 22
  • 17
1

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.

3xCh1_23
  • 1,491
  • 1
  • 20
  • 39
1

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

Ashtez
  • 21
  • 1
1

I had the same problem. My error was the packaging. So I would suggest you first check the package name and if the class is in the correct package.

1

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.

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.

Michael K
  • 1,031
  • 2
  • 14
  • 27
1

Implementing my own functional interfaces resolved this for me (so instead of using java.util.function.* just create your own single-method interface with the parameters and return-type you want).

Nathan Crause
  • 874
  • 10
  • 7
0

instanceof pattern can cause the issue in Netbeans

No issue

if (report instanceof ExecutionReport) {
   ExecutionReport r = (ExecutionReport)report;
   Usage of 'r' ...
}

Uncompilable code issue

if (report instanceof ExecutionReport r) {
   Usage of 'r' ...
}
Anatolii Shuba
  • 4,614
  • 1
  • 16
  • 17
0

I was also getting the same kind of error. As you can see at the output section of the IDE in red colors.

enter image description here

And after trying all the above solutions, except unchecking the compile after save nothing worked, which was also not producing the required output.

I was trying every possible way to do it, and finally landed to a solution. I pressed the "run file" option of the "run" button in menu section (You can press "shift+ f6" as well). It worked in my case. If anyone knows the reason behind it please add it in the comment section.

you can find the option in the image

Gayan Mettananda
  • 1,498
  • 14
  • 21