java.lang.NoClassDefFoundError - except the class exists - java

I'm using NetBeans IDE to code my Java program. A few days ago, I had it working the way I wanted to. Now that I opened it again to add some changes and comments, this java.lang.NoClassDefFoundError popped up suddenly. I have searched around but I can't seem to find a proper answer. I have set my CLASSPATH to my Java folder (*C:\Program Files\Java\jdk1.6.0_26\bin*) but it still won't work. I'm thinking of copying and pasting all my codes in a different directory but I'm afraid that the problem would still happen in the future, and changing the project's location would be too much of a hassle once the program gets really big.
In my program, I have two classes (Word and Dictionary) and a JFrame form (MainForm). Inside the Dictionary class, I have a function called testDictionary that inserts dummy data (ten Word objects) into the Dictionary object. This was running a few days ago, with no error whatsoever, but when I ran the program today, it stops on the line that says:
Word myWord = new Word();
and the java.lang.NoClassDefFoundError appears. Please help me resolve this issue, in case it appears in the future again. Thank you very much. :)
EDIT:
I have already fixed this by going to
Run > Clean and Build Main Project (Shift + F11)
then I tested it again and it worked. Though I still don't know how to ACTUALLY solve the problem.

I have already fixed this by going to
Run > Clean and Build Main Project (Shift + F11)

Related

Getting Java: unexpected type required: variable found: value all of a sudden

I'm not sure what I did, but I seem to have messed up something in my project, because it was working fine, building, compiling etc. without issues but now the build process is giving me this error:
java: unexpected type
required: variable
found: value
And what is unusual about it, is that it is calling out an argument that I am passing into a library class that I wrote and have been using for almost two years now in just about all of my projects.
It boils down to this... the method in the library has two arguments like this:
public void myMethod(double width, double height) {
this.width = width;
this.height = height;
}
and I calling the method like this:
MyClass myClass = new MyClass();
myClass.myMethod(200.0, 70.0);
When I click on the error link, it jumps straight to the first number that I'm passing into the arguments. So, since it says it wants a variable instead if I do this, I don't get the error:
double w = 200;
double h = 70;
myClass.myMethod(w,h);
These lines of code that it is throwing an error on have been in this project for months now, and have never thrown an error before. In fact, it's been compiling and running all day until suddenly it isn't.
Also, if I create a new project, using the same library, and copy and paste the exact same lines of code into the new project, it works just fine ... it's only THIS project that is suddenly getting this error, and I don't know what I did to cause it.
Does anyone have any ideas? I'm using IntelliJ ... if that matters.
I ended up "solving" the problem by creating a new project, then migrating all of my classes/resources, etc. to the new project. It now builds, compiles, and runs just fine. Still, no idea what the issue was other than some kind of corruption maybe in that project? I might hit up JetBrains to see if it's something they have seen before.
Edit:
I FINALLY found the cause of the error, and it is DEFINITELY an IntelliJ issue.
I have a class that I include in my project templates, where everything in the class is declared public static so that those resources are available to any other class in the project. There was a line in that class that somehow got messed up. This is what it looked like in damaged form:
public static final String CSS_TABLE_VIEW = Objects.requireNonNull(resource) = Objects.requireNonNull(resource.getResource("StyleSheets/TableView.css")).toExternalForm();
And this is what it is SUPPOSED to look like:
public static final String CSS_TABLE_VIEW = Objects.requireNonNull(resource.getResource("StyleSheets/TableView.css")).toExternalForm();
The error was being thrown against code that was perfectly fine in a completely different class and none of those lines of code were referencing that damaged class either.
I tried invalidating cache, and I even went so far as to completely delete everything in the project folder EXCEPT the src folder, then I created a new Project From Existing Sources pointing it to that project folder, knowing that IntelliJ would have to completely rebuild its portion of the project, and the error STILL happened in the class that was not damaged.
When I created a new project then copied the classes over, the new project had its own copy of that damaged class so I didn't copy it over, which is why creating the new project and copying the classes over to it fixed the problem.
It wasn't until I went into each class and looked at everything that I saw that damaged line in that class.
What is interesting is that after I fixed that line, and built the project, it built just fine. Then, I did Command+Z to undo the fix so that it was damaged again, but then when I built the project, the builder caught the damaged line that I had repaired then unrepaired...
So invalidating cache didn't work, nor did rebuilding the project from source files ... I had to actually fix the error one time, then the builder spotted the right problem from that moment on. I suspect if I left it damaged then did another rebuild from source, it would most likely repeat the same problem.
I've still got a case open with JetBrains so we'll see what they say about it.
Edit #2:
After some discussion with a tech at Jet Brains, I told him to close the case because I basically fixed the problem, but he was not so eager to do that, so I preserved a copy of the project that was broken to where I could just unzip a file, and create a new project from source then try to build the project which would then throw the ambiguous error in the class that had no errors.
Jet Brains asked me for a copy of the broken project which I provided and he was able to re-create the error and ended up submitting a bug report which can be seen and tracked here if anyone is interested.
Often, when I get strange and unexplained errors in IntelliJ IDEA, I go to File -> Invalidate caches -> tick all boxes -> Invalidate and restart and then the problem is fixed.

'Could not find or load main class' in Eclipse. Again and again

I'm asking this question even though it has already been asked many times because I didn't find a solution after hours of searching.
I'm using Eclipse to learn Java. Last week everything worked properly. I could run whatever I wrote. Then today, I opened Eclipse, wrote some lines and when I tried to run it, I got an error:
Error: Could not find or load main class test.ArrayTest
Caused by: java.lang.ClassNotFoundException: test.ArrayTest
But that's not all : every project in my workspace is having the same issue. I can't run anything. At first I thought it was because I made a mistake at Eclipse start, so I closed it and re-opened it, but I was in the correct workspace.
I've tried to clean/rebuild it, doesn't work.
I've tried to mess with Properties > Java Build Path, doesn't work.
I've tried to create a new project with a simple 'Hello World', doesn't work.
package Hello;
public class Main
{
public static void main(String[] args)
{
System.out.println("Hello World");
}
}
What I have in package explorer.
Console display.
The thing that suprises me the most is that I don't think I've done anything that can mess with Eclipse between the moment it was working and now.
Does someone have another idea ?
I think I found the root of the problem !
The path to my workspace was containing a letter with an accent ('Bibliothèque' by default with Windows 7+ in french...), so I tried to create a new workspace outside of it, and it worked !
I'm not a 100% sure it was really it, but since it worked, it's worth sharing.
Probably you have disconected JDK in project.
Try open conextual menu on project (right mouse button)
properties/Java Build Path/Libraries
check is correct JDK or remove
then: [Add Library] / JRE System Library/ Alternate JRE/ [Installed JREs] / [Add] / Standard VM / [directory]
select path to your JDK (not JRE) and confirm, select checkbox your new jdk and [Apply and close]
on select list choose your new jdk and [Finish] and [Apply and Close]
should works. I have sometimes this problems. It's Eclipse

Weird random errors in Netbeans

I've experienced a really weird behaviour when using Netbeans today. Firstly, I get Error: Could not find or load main class <name of my class> at random. I know what this means but I don't change anything, it appears and disappears randomly. Secondly, when I modify the code e.g. adding a line System.out.println("x"); that I am 100% sure should be executed, Netbeans seems to overlook that modification and run the old version of the code (before System.out.println("x"); was added) so as a result it doesn't get printed.
Both errors appear at random and last for some time, then disappear and appear again and so on. I've been trying to fix them for 2 hours but to no avail. They make me unable to do any work with Netbeans.
EDIT: Reinstalling Netbeans and Java didn't help. When I load the same project in Eclipse, everything works correctly.
Make sure your class has a public qualifier. Also the name of the file and the class containing the main method should be same.

Error with R.java class, layout no found but present in R.java

Well, first I'm sorry but my english is not very good because I'm French...
I work on Eclipse on an android project (pong)
So, I've a problem with an activity, when I write : setContentView(R.layout.menu);
I have an error who say : menu cannot be resolved or is not a field
The fact is... This is a file ! I went to the R.java and I see it, but the error don't disappear. (I have alredy clean and build the project many times, it still don't work)
I have took some screens ;-)
http://imageshack.us/photo/my-images/4/erreurmenupong1.png/ (In the Menu.java)
http://imageshack.us/photo/my-images/62/erreurmenupong2.png/ (In the R.java)
Delete "android.R" from your imports.
I had the same exact problem. I solved it buy making a new work space and importing all my android projects over there.

Class file name must end with .class exception in Java Search

I was hoping someone could help me out with a problem I'm having using the java search function in Eclipse on a particular project.
When using the java search on one particular project, I get an error message saying Class file name must end with .class (see stack trace below). This does not seem to be happening on all projects, just one particular one, so perhaps there's something I should try to get rebuilt?
I have already tried Project -> Clean... and Closing Eclipse, deleting all the built class files and restarting Eclipse to no avail.
The only reference I've been able to find on Google for the problem is at http://www.crazysquirrel.com/computing/java/eclipse/error-during-java-search.jspx, but unfortunately his solution (closing, deleting class files, restarting) did not work for me.
If anyone can suggest something to try, or there's any more info I can gather which might help track it's down, I'd greatly appreciate the pointers.
Version: 3.4.0
Build id: I20080617-2000
Also just found this thread - http://www.myeclipseide.com/PNphpBB2-viewtopic-t-20067.html - which indicates the same problem may occur when the project name contains a period. Unfortunately, that's not the case in my setup, so I'm still stuck.
Caused by: java.lang.IllegalArgumentException: Class file name must end with .class
at org.eclipse.jdt.internal.core.PackageFragment.getClassFile(PackageFragment.java:182)
at org.eclipse.jdt.internal.core.util.HandleFactory.createOpenable(HandleFactory.java:109)
at org.eclipse.jdt.internal.core.search.matching.MatchLocator.locateMatches(MatchLocator.java:1177)
at org.eclipse.jdt.internal.core.search.JavaSearchParticipant.locateMatches(JavaSearchParticipant.java:94)
at org.eclipse.jdt.internal.core.search.BasicSearchEngine.findMatches(BasicSearchEngine.java:223)
at org.eclipse.jdt.internal.core.search.BasicSearchEngine.search(BasicSearchEngine.java:506)
at org.eclipse.jdt.core.search.SearchEngine.search(SearchEngine.java:551)
at org.eclipse.jdt.internal.corext.refactoring.RefactoringSearchEngine.internalSearch(RefactoringSearchEngine.java:142)
at org.eclipse.jdt.internal.corext.refactoring.RefactoringSearchEngine.search(RefactoringSearchEngine.java:129)
at org.eclipse.jdt.internal.corext.refactoring.rename.RenameTypeProcessor.initializeReferences(RenameTypeProcessor.java:594)
at org.eclipse.jdt.internal.corext.refactoring.rename.RenameTypeProcessor.doCheckFinalConditions(RenameTypeProcessor.java:522)
at org.eclipse.jdt.internal.corext.refactoring.rename.JavaRenameProcessor.checkFinalConditions(JavaRenameProcessor.java:45)
at org.eclipse.ltk.core.refactoring.participants.ProcessorBasedRefactoring.checkFinalConditions(ProcessorBasedRefactoring.java:225)
at org.eclipse.ltk.core.refactoring.Refactoring.checkAllConditions(Refactoring.java:160)
at org.eclipse.jdt.internal.ui.refactoring.RefactoringExecutionHelper$Operation.run(RefactoringExecutionHelper.java:77)
at org.eclipse.jdt.internal.core.BatchOperation.executeOperation(BatchOperation.java:39)
at org.eclipse.jdt.internal.core.JavaModelOperation.run(JavaModelOperation.java:709)
at org.eclipse.core.internal.resources.Workspace.run(Workspace.java:1800)
at org.eclipse.jdt.core.JavaCore.run(JavaCore.java:4650)
at org.eclipse.jdt.internal.ui.actions.WorkbenchRunnableAdapter.run(WorkbenchRunnableAdapter.java:92)
at org.eclipse.jface.operation.ModalContext$ModalContextThread.run(ModalContext.java:121)
Thanks McDowell, closing and opening the project seems to have fixed it (at least for now).
Comment #9 to bug 269820 explains how to delete the search index, which appears to be the solution to a corrupt index whose symptoms are the dreaded
An internal error occurred during: "Items filtering".
Class file name must end with .class
message box.
How to delete the search index:
Close Eclipse
Delete <workspace>/.metadata/.plugins/org.eclipse.jdt.core/*.index
Delete <workspace>/.metadata/.plugins/org.eclipse.jdt.core/savedIndexNames.txt
Start Eclipse again
Two more general-purpose mechanisms for fixing some of Eclipse's idiosyncrasies:
Close and open the project
Delete the project (but not from disk!) and reimport it as an existing project
Failing that, bugs.eclipse.org might provide the answer.
If the workspace is caching something broken, you may be able to delete it by poking around in workspace/.metadata/.plugins. Most of that stuff is fairly transient (though backup and watch for deleted preferences).
Got this error to the other day. Tried deleting the all .class-files and resources from my output folder manually. Didn't work. Restarted my computer (WinXP). Didn't work. Closed my project in Eclipse and opened it again. Worked!!! Hopes this solves someones problem out there. The search facilities and truly essential to Eclipse.
I also encountered this issue recently, the below scenario worked for me.
Close Eclipse
Delete <workspace>/.metadata/.plugins/org.eclipse.jdt.core/*.index
Delete <workspace>/.metadata/.plugins/org.eclipse.jdt.core/savedIndexNames.txt
Start Eclipse again
Closing the projects didn't do the trick for me. I started eclipse with the -clean flag and that worked for some reason.
Just
Close project
Clear manually output folder(s)
Open project
(Eclipse 3.5 SR2, Build id: 20100218-1602)

Categories

Resources