Weird random errors in Netbeans - java

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.

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

Whenever I hit run eclipse it gives value of previous run program

Earlier when i am creating a new class for practicing programs in Eclipse. I use to create in same project and it used to run well. But lately i have to specially go to Run>Run Configuration> I have to manually select the project and main class.
Is there any configuration i m missing as this is time consuming to every time go and change the main class.
Can anyone help?
You can press the black arrow near the Run button to select what to run, usually you will find your classes with public static void main(String[] args) there. That's how it works for me.
The problem can be due to new class files not being generated due to errors. I have seen the same results as you (new changes not picked up) due to one or both of the following.
1) Look for red-! or red-X next to a project and fix the problem(s) in that project.
2) Check the Problems tab and fix all red-X errors.

Error: Could not find or load main class in intelliJ IDE

I'm a beginner in Java and am trying to run my code using IntelliJ that I just installed as my IDE with JDK 1.7. The following piece of code keeps does not even compile and keeps giving me the error:
Error: Could not find or load main class libTest
Code
import java.lang.Integer;
import java.lang.String;
import java.lang.System;
import java.util.*;
class book {
private String name = "trial";
private int bookCode=1;
private int issued=0;
public void Issue(){
if(issued==0) {
issued=1;
System.out.println("You have succesfully issued the book");
}
else {
System.out.println("The book is already issued. Please contact the librarian for further details");
}
}
public int checkCode() {
return bookCode;
}
String readName() {
return name;
}
public void setName(String newName){
name=newName;
}
public void setBookCode(int newCode){
bookCode=newCode;
}
}
class library {
private ArrayList books=new ArrayList();
public void getList(){
for(int bk:books){
String bName=books(bk).readName();
System.out.println((bk+1)+") "+bName);
}
}
}
public class libTest{
public static void main(String[] args){
library newLib= new library();
System.out.println("code working");
}
}
Is there any change that i have to make in the compiler settings?? Or is it the code.
This might help:
1) "Build" menu -> "Rebuild Project".
Sometimes Intellij doesn't rewrite the classes because they already exist, this way you ask Intellij to rewrite everything.
2) "Run" menu -> "Edit configuration" -> delete the profile -> add back the profile ("Application" if it's a Java application), choose your main class from the "Main Class" dropdown menu.
3)"Build" menu -> "Rebuild Project".
If none of the above answers worked for you, just close your IntelliJ IDE and remove the IntelliJ IDE file and folder from the root of your project:
rm -rf .idea *.iml
Then open the project with IntelliJ. It must work now.
For me the solution was to fix the output directory under project settings. Before I was using just "target" for the Project compiler output. Instead I updated it to have a full path e.g. D:\dev\sigplusjava2_68\target
I had this problem and I tried everything under the sun that I could think of and on this site.
None of my Java classes were being picked up after I pulled from a remote branch. All the classes had red Js by their names in the Project Hierarchy, not blue Cs.
In the end, I tried to follow this tutorial and a few steps in tried something not described and fixed the issue:
https://www.jetbrains.com/help/idea/creating-and-managing-modules.html
Here's what I did:
Goto File | Project Structure, or press Crtl+Shift+Alt+S
Select Modules under the Project Settings section.
In the Sources tab click Sources on the 'Mark as:' line.
Click the Apply button.
For some reason, all my classes then had blue C's.
Someone with a better understanding of how IntelliJ and/or IDE's might be able to explain the phenomenon, but all I know is now it can see all the classes and more importantly the main one, and run.
Invalidate cache and restart your IntelliJ, it worked for me.
Explicitly creating an out folder and then setting the output path to C:\Users\USERNAME\IdeaProjects\PROJECTNAME\out
seemed to work for me when just out, and expecting IntelliJ to make the folder wouldn't.
Also try having IntelliJ make you a new run configuration:
Find the previous one by clicking
then remove it
and hit okay.
Now, (IMPORTANT STEP) open the class containing your main method. This is probably easiest done by clicking on the class name in the left-hand side Project Pane.
Give 'er a Alt + Shift + F10 and you should get a
Now hit Enter!!
Tadah??
(Did it work?)
File > Project Structure > Modules > Mark "src" folder as sources.
This should fix the problem. Also check latest language is selected so that you don't have to change code or do any config changes.
I know this was asked a while ago, but I was just stumbling over this issue and thought my findings might help others. As pointed out, the error message is basically a result of the out folder. That's because, when you're trying to run the program, it compiles the code first, and puts the compiled result to the out location, and then it tries to load the compiled code from the out location. If the compiled code is not in the location expected, you'll get the error.
The point I'm particularly wanting to share is that some times, the code is not compiled (built), even though your run configuration specifies "Build" in the "Before launch" section of the configuration panel.
When can this happen?
One situation that can cause this to happen is if you're using modules and you manually delete the module out directory. For example, if I have a module named "foo", there should be a directory named foo under out/production. If you manually delete it, the build system may not know that it needs to be rebuilt.
Even worse, if you select Build | Build module 'foo', it still may not rebuild the module. If that's the case, you should select a file in the module, for example 'bar.java' and then select Build | Recompile 'bar.java'. Now the out directory out/production/foo should be restored.
Since IntelliJ typically knows about any changes going on, this surprised me, and took me a little time to figure out, so I thought I'd share.
Check your class module : I have encountered this problem with intellij :
I have a maven multi-module project, the problem is that i runing a class which not exist the module within the configuration, so my problem is fixed by setting the right module ("edit configuration" -> "use class of module")
may this help you
I had to mark the "src" folder as "Sources". After restarting IntelliJ and rebuilding the project I could run the project without further issues (see screenshot).
Edit: You can access the "Project Structure" tab via File->Project Structure or by pressing Ctrl+Shift+Alt+S.
I ran into this problem when my Java class was under src/main/kotlin. After I moved it to src/main/java, the problem was gone.
I have faced such problems when the class is in the default folder, i.e. when the class does not declare a package.
So I guess using a package statement (eg. package org.me.mypackage;) on top of the class should fix it.
Open Modules Tab (Press Ctrl+Shift+Alt+S). I had two modules under one project. I've solved the problem after removing the second redundant module (see screenshot).
After creating your project in intelliJ, try running the following command:
mvn package
I have tried all the hacks suggested here - to no avail. At the end I have simply created a new Maven application and manually copied into it - one by one - the pom.xml and the java files and resources. It all works now. I am new to IntelliJ and totally unimpressed but how easy it is to get it into an unstable state.
Invalidating cache didn't work.
I edited the main class java file with a dummy change and ran it. It worked.
In my case the problem seemed to be related to upgrading IntelliJ. When I did this I overwrote the files from the old IntelliJ with the files from the new IntelliJ (2017 community to 2018 community). After that all of my projects were broken. I tried everything in this thread and none of them worked. I tried upgrading gradle to the latest version (4 to 4.8) and that didn't work. The only thing that worked for me was deleting the entire IntelliJ folder and reinstalling it. All of my projects worked after that.
I have tried almost everything suggested in the answers here, but nothing worked for me.
After an hour of just trying to run my application, I noticed that my project's path included non-ASCII characters (Arabic characters). After I moved my project to a path with no non-ASCII characters, it executed just fine.
Goto File-> Invalidate Caches and Restart .
Else delete rm -rf .idea *.iml
and restart InteliJ
You can run the maven command on the pom.xml file in your project directory:
mvn clean install
For me - i tried few of the options above, did not work. Then i just renamed my Application class and that probably forced intelliJ to build a fresh jar and error message started to change. Then i renamed it back and it worked.
Mark the directory as a source directory. Opened via Ctrl+Shift+Alt+S
modules.xml with wrong content, I don't know what's matter with my IDEA.
I inherited a bunch of .JAVA files from elsewhere and couldn't figure out how to get them to work in any IDE. Ultimately I had to go to the command line where the Main.JAVA file was and run javac Main.java. This created a bunch of .CLASS files. The IDE's were then able to figure out what to do.
I got this error when using Scala/SBT. IntelliJ could not find the main class, even though everything was set up correctly.
My solution: delete the <user>/.sbt/<version>/plugins/target folder, then restart IntelliJ.
You probably would have specified a wrong package and the package hierarchy would not be right. Look below
The ide would highlight the wrong path in that case.
I'm using IntelliJ with Spring and my main class is wrapped in a JAR.
I had to mark the 'Include dependencies with "Provided" scope' in the Run/Debug configuration dialog
We are at File/Project Structure..
Answer might be:
Folder indicated as "content root" needs a child folder where the code is.
Plus find the button that marks code as excluded and not.
Not to be confused with tickbox that states excluded without telling in what phase and what**
Is it compiler exclude or runtime exclude? You are doomed to test and lot.
So no that tickbox but icons and colors.
As an idea we need to crack how it was originally thought to work. They never got it to work in first place and started add things in premature codaculation style.
It has been so many years and you cannot expect any improvement.
But as cure we can hack out some way to get it right every time.
Another thing you can check here is the actual command that is being passed to the JVM and make sure it looks OK. Scroll to the top of your Run console, it should be the first line.
Spaces in your Run Configuration VM Options field will malform the app startup command and can result in this error message
-DsomeArgument="arg with space must be quoted"
I am working with Kotlin but am guessing the problem is the same. I would start a project, create a single file and add main to it and the IDE couldn't find the main.
I tried the things in this list and none worked. I finally mentioned my frustration on one of the IntelliJ pages and was contacted. Of course, it worked fine for IntelliJ. After a couple of days back and forth, I noticed that the highlight function wasn't working and mentioned that. It turned out something was wrong with the IDE settings. I still don't know specifically what was wrong but the fix in my case was to reset the IDE settings. File->Manage IDE Settings->Restore Default settings.
After this, the green triangle start icon became visible to the left of my main function and things continued to work normally for subsequent projects.
Thanks to Konstantin at JetBrain's support for his patience.

java.lang.NoClassDefFoundError - except the class exists

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)

Categories

Resources