public class Sequence {
public static void main(String[] args) {
int limit=20;
int sum=0;
int a=0;
int b=1;
while(sum<limit)
{
sum=a+b;
a=b;
b=sum;
}
}
}
My Netbeans was working fine when I was using jdk 1.8.0.65 earlier but due do some reason I lost my data, I installed jdk 1.8.0.73 then and now my every program on Netbeans shows this error:
run: Error: Could not find or load main class sequence.Sequence
C:\Users\owais\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53:
Java returned: 1 BUILD FAILED (total time: 0 seconds)
I googled the problem and updated the netbeans_jdk path in netbeans.config file but still it does not work.
I am new to programming I would really appreciate some help.
Actuall, when you click the "Run" button on top of the netbeans window, it only run the application's main class . but this Sequence class is not the main class of the project most probably. for this case to run this class you have to right click on Sequence class editor and select Run or Run as menu. Then this class will be executed on separate console, I hope this will work ..
I have been a victim of this problem, but the project I was working on was a bit large and I happen to use multiple Jpanel under one JFrame to develop the project so the code was over 15,000 lines of codes. So this seem to happen when I build the project, the code were scanned but because of the long code under the main class, it fail to reach the end of the code and raise the fail to load the main class. This is how I solve the problem using the Netbeans IDE:
Remove unnecessary components and codes to reduce the code size.
Right Click on the project name, then click refactor and rename the project name.
Also rename both the classes and jframes in the project.
Click Run Then Click Clean and build project.
If main class is correctly specify, try this.
Two problems I can identify:
File name has special characters. Remove those special characters.
Folder where the project was located has special characters. Remove those special characters too.
These will solve the problem:
Error: Could not find or load main class Classes.Test
/..../...../Library/Caches/NetBeans/8.2/executor-snippets/run.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds)
Related
yesterday i was trying to run a simple Java class, with a simple method void to print something, when i want to run that class i got something weird... either Eclipse IDE run a different class (a previous one) or don't run at all, just asking for a main class from that package... but why? I mean, if i create a new Maven project, or a new Class, i expect to run that class, but if i want to Run as -> and searching for Java Application, doesn't apper. Anytime i want to run something, either i neet to run that main class from that particular project/package created or Eclipse running a whole different class from a few days ago when all was good. The same thing i occur in tool Suite...
If you need some photos of configuration or something, feel free to ask, right now i don't know what to do...
Edit: I created a Maven project with a new Class, TestVoidClass with a void method that prints: System.out.println("Inside TestVoidClass");
So now i have two classes in my MavenProject, App class with main method who works fine and i can run that hello world print, and TestVoidClass, who doesn't work, doesn't appear to run as Java Application... bellow are some photos
First photo showing that it's running a previous class:
Second photo showing i can't find that Run as Java application:
Working with default App maven project main class:
make sure the class you want to run contains a main-Method. That is:
public static void main(String[] args) {...}
If a class doesn't contain this method, it can't be run. In your main-Method, you can call your Sysout() function.
If you open a class containing a main-Method, Eclipse will automatically run it when you click Launch.
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
I'm working on a Java project that I'm developing in Eclipse. Till today everything was fine. Yesterday before finishing working on my project I run it one last time to check if everything is OK and it was running correctly. But today when I fired up the project and pressed "Run" my app misterously just closes (No crashes, no messages, nothing). I traced back the issue and it turns out that the issue is "new JFrame()" when called it just closes the app.
I created a test class in the same project, you can see it below:
import javax.swing.JFrame;
public class asdasd {
public static void main(String[] args) {
System.out.println("A");
try{
JFrame frame = new JFrame();
}catch(Exception e){
e.printStackTrace();
}
System.out.println("B");
}
}
When I run this code the console just outputs:
A
B is never shown!
On the other hand, if I create a completely new project and I copy paste that class the output is as it should be:
A
B
I tried changing the default JRE, clearing the bin folder, and so on.
The only working fix so far is to remove random JARs from my class path. I have 16 JARs in total in my projects class path. But if I remove randomly some of theese JARs then it starts working, breaking completely my project of course. It doesn't matters what JARs I remove, after removing some amount of them it starts working.
Things I tried so far:
- Remove all Classes in my project leaving only the test class = Same Result
- Create a fresh project, copy-paste all my classes and dependencies = Same Result
One curious thing is that if I compile my code into a JAR file and run it from the CMD, Swing starts normally and my app works as expected. AB is shown correctly. So it must be something Eclipse related. But I haven't updated anything for it to brake today. (I only updated my Nvidia GPU Drivers yesterday night, but that is completely unrelated to Eclipse).
Anybody has an idea what could be causing this issue? Thanks.
I had absolutely identical situation.
My code crashed inside swing Window class during JFrame creation with no exceptions thrown. Test code looks like this
import javax.swing.JFrame;
public class MainPanel {
/** A Start point of the BC UI Clien Application. */
public static void main(String[] args) {
try {
JFrame objLoginPane = new JFrame();
objLoginPane.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
objLoginPane.pack();
objLoginPane.show();
} catch (Throwable ex) {
ex.printStackTrace();
}
}
}
In my workspace there was 2 projects with swing GUI. More simple one worked as it should more complex one crashed on JFrame creation.
In new workspace situation was the same. Solution above did not helped.
During further investigation I've figured out that the project began to crash when certain amount of libraries (19-22 in my case) included in build path. It was not depend on size of jars in build path or type. There was no clear logic with what jars it stops to work.
So long story short I've found the crash reason in windows journals it's NVIDIA 3D Vision Driver! After 3DVision driver uninstall all works fine (it is not required to uninstall display driver only 3D Vision cause the problem). I have latest (v378.49) GeForce drivers installed. There is my rough translation of windows journal entry related to this error:
Failed module name: nvSCPAPI64.dll, version: 7.17.13.7849, time stamp: 0x588218a5
Error code: 0xc0000409
Error offset: 0x0000000000034b2f
Failed process identifier: 0x143c
Failed process start time: 0x01d280cd39a31077
Failed process path: C:\Program Files\Java\jdk1.8.0_121\bin\javaw.exe
Failed module path: C:\Program Files (x86)\NVIDIA Corporation\3D Vision\nvSCPAPI64.dll
Report identifier: 6dd65788-acb1-48ce-a786-4e38af325fec
So I finally found a working solution for me. I created a completely new Eclipse Workspace and copy-pasted all my classes. This seems to have fixed the unknown issue. I still don't know what caused the issue, but I hope it won't happen again.
This is not a real solution but more a workaround. I guess the best way would be to use Version Control as #efekctive suggested.
Thanks everyone to have provided suggestions on how to approach this issue.
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.
I'm running Java program in Eclipse and I'm calling one class from another class. if I run the class that contains the main method it gives an error like:
could not find main class. program wil exit
A sample code is:
public static void main(String[] args)
{
Test t1=suitToRun();
TestRunner.run(t1);
}
public static Test suitToRun()
{
TestSuite suite= new TestSuite();
suite.addTestSuite(Login.class);
return suite;
}
This usually happens when your JAVA file has not been successfully compiled (to the .CLASS). Common fixes to this is to CLEAN (Menu > Project > Clean...) your project or rectify your JRE setting in Eclipse (or your Project, if its project specific).
You can quickly check this by right-clicking on the Java file, Run As > Run Configurations.... If you have an error, you should see an error in the dialog box that pops-up (right at the top).
Go to Run -> Run Configurations..
Make sure u have written name of Main class and Project in their respective places on right side of box..Main class option just requires name of the class containing main function and project option requires name of ur project..(take care of lower n upper cases)..
;)
Now..
If at the top of box if u can see the error
" [JRE]: JRE not compatible with project .class file compatibility: 1.7 "
then u r lucky as it can be rectified.. it is thr becoz ur Compiler Compliance level is set to the version of JAVA which is not there in ur computer or not included in Eclipse..in this case '1.7'
so u just need to set it to low level (1.6, if it is already set to 1.7)..to do that,just follow d followin path..
Project-> Properties
Click on Java compiler option in the left column n set ur desired Compiliance level shown on the right side then press ok and yes to the pop up and run it from the toolbar with ur fingers crossed...
hope it helps :)