Netbeans - class does not have a main method - netbeans

My program is just a simple System.out.println("");
But netbeans cannot find the main method.
Is netbeans 6.7.1 conflict with WIN7?
Any possible error?

This happens when you move your main class location manually because Netbeans doesn't refresh one of its property files.
Open nbproject/project.properties and change the value of main.class to the correct package location.

Sometimes passing parameters in the main method causes this problem eg. public static void main(String[] args,int a). If you declare the variable outside the main method, it might help :)

Exceute the program by pressing SHIFT+F6, instead of clicking the RUN button on the window.
This might be silly, bt the error main class not found is not occurring, the project is executing well...

It was most likely that you capitalized 'm' in 'main' to 'Main'
This happened to me this instant but I fixed it thanks to the various source code examples given by all those that responded. Thank you.

This destroyed me for a while.... I knew that there HAD to be an easier way with a world class IDE like Netbeans.
The easiest method is to press Shift+F11 (Clean and Build Project), then hit F6 to run it.
It refreshes Netbeans appropriately and finds your main without all the manual labor; and if you have multiple mains, it will give you the option to select the correct one.

Make sure it is
public static void main(String[] argv)
No other signature will do.

in Project window right click on your project and select properties go to Run and set Main Class ( you can brows it) . this manual work if you have static main in some class :
public class Someclass
{
/**
* #param args the command line arguments
*/
public static void main(String[] args)
{
//your code
}
}
Netbeans doesn't have any conflict with W7 and you can use version 6.8 .

If you named your class with the keyword in Java, your program wouldn't be recognized that it had the main method.

I had this issue as well (Error: Could not find or load main class test.Test). I'll explain this relatively basically since I know I would have appreciated someone doing that when I was looking for my answer.
When I right-clicked on the project (left hand side of the screen unless you got rid of the projects tab) and went to properties and then run, the main class had the projectname.classname, which is what confused me.
For example, I created a project "test" to test this out, and when I went to
(right-click) test or Source Packages -> properties -> run -> main class
had Test.test in that field, which is what the problem was. the class is test, not Test.test, so I clicked browse to its right, and the only thing in the list to select from was test, and when I selected that and tried rerunning it, it finally worked.
Additionally, I found that when you create a new project in Netbeans, one of the things it originally gives you (in my case of the project named test) is package test;. If you are having this problem, then like me, you probably originally got rid of that line seeing it as just another line of code you didn't need. That line of code is what enabled your main class which in my case was Test.test to find the main class *test from that.

My situation was different I believe because non of the above solutions di work for me. Let me share my situation.
I am importing an existing project (NewProject->Java->Import Existing Projects)
I name the project to xyz. The 'main' function exists in Main.class.
I try to run the code I modified in the main function but the error pops out. I tried the shift_f6, specifically rebuild. Nothing works.
Solution: I took the project properties and saw the 'Source Package Folder' mappings in the Sources branch was blank. I mapped it and voila it worked.
Now anyone might think that was very silly of me. Although I am new to Java and Netbeans this is not the first time I am importing sample projects and I saw all of them had the properties similar. The only difference I saw was that the main class was not having the name as the project which I believe is a java convention. I am using JDK7u51 (latest till date), is it causing the issue? I have no idea. But I am happy the project is running fine now.

Check for correct method declaration
public static void main(String [ ] args)
Check netbeans project properties in Run > main Class

While this may be an old question, the problem is still occurring these days, and the exact question is still not answered properly.
It is important to note that some projects have multiple classes with a main method.
In my case, I could run the project via the main class, but I could not run a particular other class that had a main method.
The only thing that helped me was refactoring the class and renaming it.
I've tried:
restart NetBeans
re-open the project
clear NetBeans cache
delete the file and create a new one with same name and contents
delete the file and create a new one with same name but very simple contents with only main method and print out message
rename the class (refactor) so a temp name and back
delete the project and create a new one with the same sources
The only thing that let me run this class is renaming it permanently. I think this must be some kind of a NetBeans bug.
Edit: Another thing that did help was completely uninstall Netbeans, wipe cache and any configuration files. It so happened that a newer Netbeans version was available so I installed it. But the old one would have probably worked too.

public class TestConnection {
public static void main(String args[]) {
//make sure when you create a new class, it has the above line, and then
//when you click shift+F6, it works fine for NetBeans IDE 12.4
myConnection my = new myConnection();
my.getConnection();
}
}

Related

Why it compiles with different type?

Error:
IL2CPP error for type 'Namespace.SubNamespace.MyClass/<MyIEnumeratorFunc>d__20' in assembly 'Path\MyUnityProject\Temp\StagingArea\Data\Managed\VisualStudioSolutionName.dll'
Additional information: Interface System.Collections.Generic.IEnumerator`1<System.Object> method T System.Collections.Generic.IEnumerator`1<System.Object>::get_Current() not implemented on non-abstract class Namespace.SubNamespace.MyClass/<MyIEnumeratorFunc>d__20
Unity expect System.Collections.IEnumerator and I try to use IEnumerator:
I tried to write directly
private System.Collections.IEnumerator MyIEnumeratorFunc(){}
I tried to remove
using System.Collections.Generics;
and append it to each List/Dictionary entry
Everything works in Editor, but I get error I described above:
Seems like it tries to use System.Collections.GENERICS.IEnumerator.
Also, the error disappears if I use source code (not .dll)
Also, you can reproduce it by creating .dll with any IEnumerator/async function (even empty) and build for iOS.
The problem was in Project name.
Solution is
Right mouse click on Project name (not solution name) in Solution Explorer.
Properties
"Application" tab
Assembly name -> set to the same to .dll file name (without ".dll")

NoClassDefError for JavaFX MenuBar

I'm just starting on a large new project, containing the line menuBar = new MenuBar(); MenuBar is packaged in javafx.scene.control. No errors, but when it comes across this line at runtime I get
Exception in thread "AppKit Thread" java.lang.NoClassDefFoundError: Could not initialize class javafx.scene.control.MenuBar
Google suggests there's a discrepancy between my build path and my runtime path. What I'm not sure about is how to actually tell what the build paths and runtime paths are, although I think I have it right.
Build path: I go to
Properties -> Java Build Path
and see that JRE System Library [Java SE 8 [1.8.0_77]] is there. I furthermore confirm that it contains jfxrt.jar. It also detects no errors at build time so I can assume it's working just due to that.
Runtime path: I go to
Run As -> Run Configurations... -> Classpath
and see that JRE System Library [Java SE 8 [1.8.0_77]] is listed in Bootstrap Entries. I don't seem to be able to expand that and confirm that it contains the relevant jar.
Am I missing anything in the runtime classpath?
EDIT: I found another symptom, or a nuance to this. The menuBar = new MenuBar() is contained in the constructor of an object that extends the FX Stage class. The constructor is initialized by clicking a button on a launcher. When I click it the first time, I get a Exception in thread "AppKit Thread" java.lang.ExceptionInInitializerError. The second time I click it is when the NoClassDefFoundError shows up. Does this mean anything?
EDIT 2: Still trying to figure out this problem. I've possibly narrowed it down to an issue with Style. I don't know what that means, really, but the trace may help:
Caused by: java.lang.NullPointerException
at com.sun.javafx.css.StyleManager.getURL(StyleManager.java:867)
at com.sun.javafx.css.StyleManager.loadStylesheetUnPrivileged(StyleManager.java:1057)
at com.sun.javafx.css.StyleManager.loadStylesheet(StyleManager.java:917)
at com.sun.javafx.css.StyleManager._setDefaultUserAgentStylesheet(StyleManager.java:1377)
at com.sun.javafx.css.StyleManager.setUserAgentStylesheets(StyleManager.java:1209)
at com.sun.javafx.application.PlatformImpl.lambda$_setPlatformUserAgentStylesheet$182(PlatformImpl.java:698)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl._setPlatformUserAgentStylesheet(PlatformImpl.java:697)
at com.sun.javafx.application.PlatformImpl.setPlatformUserAgentStylesheet(PlatformImpl.java:548)
at com.sun.javafx.application.PlatformImpl.setDefaultPlatformUserAgentStylesheet(PlatformImpl.java:512)
at javafx.scene.control.Control.<clinit>(Control.java:87)
... 6 more
I don't know how to follow past those ...6 more, but I can manually trace through to the JFX MenuBar class to find it in the MenuBar constructor: getStyleClass().setAll(DEFAULT_STYLE_CLASS); seems like a good candidate for where this is happening. I'm not sure how to proceed because this is in the .class file itself. Hopefully this helps.
EDIT 3: I was given the advice to just create a new workspace, check out the projects from SVN, and just start over. The thought was that maybe somehow the workspace was corrupted. Nothing changed; the exact same error has shown up. I am running out of ideas here.
EDIT 4: (thanks for the formatting, Itachi!) Here is what I can show, obscured:
package _______;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.image.Image;
import javafx.scene.paint.Color;
import ____.FooMenu;
import ____.FooMenuItem;
import ____.FooUtil;
import ____.FooVBox;
import ____.FooStates;
public class FooStageWithMenu extends FooStage
{
private MenuBar menuBar;
private FooMenu fileMenu;
private FooVBox root;
public FooStageWithMenu(FooStates setting, Image icon)
{
super();
set(setting, icon);
root = new FooVBox();
menuBar = new MenuBar(); //This line is where it fails
root.addNode(menuBar);
/*...*/
}
/*...*/
}
A few other thoughts:
I'm developing on OSX, everyone I've spoken to on the project is using Windows. Could there be an OS-specific source of this problem?
We wanted to eliminate the possibility of library issues, so I installed the most recent version, now running JDK 1.8.0_102, problem still persists.
Is there some chance that even in the most recent JDK, there's some discrepancy between the jfxrt.jars in our respective libraries?
The nature of the specific error (see EDIT 2 above). It seems like there's an attempt to access a style sheet by retrieving a URL, but it either fails in accessing that style sheet, or in getting the URL, or something. Nothing about the NullPointerException seems to help me narrow it down past that. I did explore my jfxrt.jar and found that in the paths the JavaFX classes seem to be searching for, the .css files do exist and seem complete and non-corrupt.
EDIT 5: So I'm pretty sure nobody's reading this, but I did just find a big clue, related to the difference between this version and the previous. I don't think I need to get into the specifics, but this problem is "fixed" by commenting out one line in the launcher class. We have two launcher classes, one of which extends the other. The constructor for the extended class is just super(); new JFXPanel(); If I comment out the second line, it works without any immediately apparent problems. This might be the line that breaks it on a Mac, while it works fine in Windows. I have no idea what this implies, and I have no idea why the original failure I shared was caused by this. But this change is just made in a test branch, so it may be that we have no need to give this problem a proper fix, given the temporary and limited nature. I just wanted to share the update in case it helps anyone else.

Newbie IDE (IntelliJ IDEA) issue: .class files not all usable

I'm working on a school project right now, and every time I have in the past, I always make a new Project in IntelliJ IDEA. However, this time she gave us some .class files that have methods in them that we can't see (she described what they do so we know how to use them) but need to use, so they have to be in the same folder, obviously.
SIDENOTE: I'm also new to using Macs, which is what I'm on at the moment.
Anyways, I put the .class files in my src folder that I found in the Project8 folder. I just made an array of the Book objects, which was one of the .class files I moved, and now I need to use a method from the other .class file, named BookInventory.class. I put that file in the src folder just like the other, but it won't let me use the only method in that class, which is LoadBooks.
Here is the LoadBooks signature:
public static void LoadBooks(Book[] b)
And here's the description of it that she gave to us:
"For each element in the array, accepts user input for the book, creates the Book object, and stores the object into the array."
So, when I made the array of Book objects, IDEA made an import statement up top all by itself, I didn't type it:
import java.awt.print.Book;
So why does IDEA recognize the Book.class file and allow me to use it in this .java file for my project, but it doesn't seem to notice the BookInventory.class file?
Any help appreciated, thanks ahead of time.
What is happening is when you first typed the line with LoadBooks(Book[] b), IntelliJ could not "see" your class files (you have subsequently loaded them in "class files" and added that as a project library, I presume).
IntelliJ however searched for and found a Book class in the internal java libraries, java.awt.print.Book. Note that this is a different class to the one your teacher gave you, which might have been e.g. edu.myschool.homework.Book.
Firstly, try to delete the line including the import statement, or manually change it to the correct package (your teacher can inform you what it is).
If the same import comes back automatically, you can go into Settings -> Editor -> General -> Auto Import and untick Add unambiguous imports on the fly - this will cause intellij to prompt you before adding imports.
Also, I would ask your teacher to give you the class files in a jar file, since that's the usual approach.
Good luck.

Eclipse C++ formatter puts new line before method identifiers

I ran into a problem with the Eclipse formatter. It won't format my code correctly when declaring methods within a class declaration. It puts a new line after the method's return type.
I already exported the style xml file and examined the settings in it, but none of the settings have any apparent connection to this problem, and the settings editor in Eclipse didn't show the same problem happening in it's sample code for method declarations.
Here is an example bit of code for what I want to have happen:
class MyClass
{
public:
MyClass();
void myMethod();
};
However, this is what I get:
class MyClass
{
public:
MyClass();
void
myMethod();
};
Again, in the styles editor, the code doesn't have this problem and looks just how I want it to, but in the actual code, the story is different.
I'm using version 3.8.0. Any help is appreciated.
Edit: I deleted those source files that were formatted incorrectly (after formatting the code several times to no avail) and replaced them with "identical" files with the same methods, same structure, etc. I formatted the code this time and it worked. This is probably a bug, but I'm leaving it up just in case anyone else encounters a similar problem or has a solution to avoiding this problem in the first place.
I hand edited two files under the main eclipse projects directory
.metadata\.plugins\org.eclipse.core.runtime\.settings
The two files:
file 1: org.eclipse.cdt.core.prefs, change this line from "insert" to "do not insert"
org.eclipse.cdt.core.formatter.insert_new_line_before_identifier_in_function_declaration=do not insert
file 2: org.eclipse.cdt.ui.prefs,
scan this file for "insert_new_line_before_identifier_in_function_declaration" and make a similar change from insert to do not insert next to it, should be obvious
Note I seen this problem on indigo and juno, the fix described above was in juno.
If you have a custom formatter config, export it first (settings>C/C++ General>Formatter>Edit>Export). Then change the following line to "do not insert". Save the XML.
<setting id="org.eclipse.cdt.core.formatter.insert_new_line_before_identifier_in_function_declaration" value="do not insert"/>
Delete the current config and import the one you changed.
There's a specific preference in the formatter options starting from cdt 9.8 included in Eclipse 2019-06.

Why eclipse is generating argument names as arg0,arg1,arg2.... for methods?

When I try to access some class's method; eclipse gets that method but arguments inside that method are replaced by arg0, arg1, arg2...
Example:-
Suppose I have this method in some class named ReadFile.java
#Override
public int readXXX(int start, int end, String xxx) throws IOException {
return 0;
}
When I try to access that method from an instance of class it shows me as follows:
readFile.readXXX(arg0, arg1, arg2);
It becomes hard to identify what I should pass in argument. The same thing also happens for java methods. i.e. when I implement interface all method under that will be generated but arguments in that methods are arg0, arg1.....
Is there any setting I can do prevent this?
I'm using Eclipse Galelio.
Eclipse can't work out the arguments because it can't find the source or javadoc attachments.
Make sure your source is attached.
To check, click on the method call readXXX and press F3 which should open the method declaration. If the source is not attached, Eclipse will say "Source not found" and will allow you to "Attach Source...".
Anyone having the same issue, try performing a Project > Clean, this will delete the old class files, and Eclipse will recompile them in debug mode this time. Worked for me in Indigo 3.7.2
The problem is that your class files lacks debug information embedded in them. JDT doesn't reparse the source files or the javadoc for dependencies, when building the AST for your project, so it doesn't have idea what the name of the parameter is, even when you are able to open the class and clearly see what are the method names.
In order to correct this, you need to recompile your class files with debug information enabled, which makes the class file considerably larger, but unless you are developing for memory-constraint devices, you should be fine.
for those like me who tried to apply one of our colleagues suggestions and unfortunately hasn't worked, please, give a try to check the option "Add variable attributes to generated class files (used by the debugger)" within Window -> Preferences -> Java + Compiler.
Once you've done that, try to build the project again.
Cheers.
I solved this by going to preferences
Java / Content Assist
On this page under "Sorting and Filtering" I unchecked "Hide proposals not visible in the invocation context" now instead of seeing (arg0, arg1, arg2) etc in autocomplete I see (catalog, schemaPattern, tableNamePattern...)
I am using Spring Tools Suite 3.7.2 which runs on the Eclipse Mars 4.5.1 platform.
This link helped me to solve this problem.
1) Right click on your android.jar and select Properties.
2) Select Java Source Attachment. Enter the source directory location (you can also use External Folder… to browse to the directory) and click on “Apply“.
The code names match the following version numbers, along with API levels and NDK releases provided for convenience: