Eclipse Java Error: Cannot find or load main class - java

I am using Eclipse Luna and i am trying to run a simple text program but Eclipse gives the error:
Error: Could not find or load main class Main
I have no idea what's going on. I haven't found anything useful on Stack Overflow or Google. Here is my code:
public class Main {
public static void main(String[] args) {
System.out.println("This is a String");
}
}
Thank you.

Your code looking fine !
Do following steps to run your class
Select your project, go to Project section in menu bar then click clean.
In tool section click on enter code here Build Automatically.
Select your class right click on it and then select run.

In the menu Run, select Run Configurations....
In the dialog box that opens, on the left side, you are supposed to have an entry Java Application, and under it, there should be your class (and maybe other classes you worked on before). It may be named Main or Main__ here.
Select it. If it was erroneously named Main__, change that to Main in the Name: field.
Now look at the entry for Main Class. In the text field, it should say Main. If it says Main__ or anything else, change that to Main.
Apply and Run.

Related

Using Jdf and Intellij: Specifically running a Jfd form in intellij

I don't really understand how to integrate jfd and intellij.
So far I have played around with it a little bit and developed a program that looks .
I then generated an actionEventHandler to handle when the add package button is pressed. For now I just kept it simple and had it output "THIS IS A TEST" into the outputField which is the textField in the picture above.
the actionEventHandler looks like this:
private void addPackageButtonActionPerformed(ActionEvent e) {
outputArea.setText("THIS IS A TEST");
}
I would then expect when I did "Test Form" on the jfd tab I would click the addPackageButton and it would put "THIS IS A TEST" into the outputField.
However, nothing happens. Is this what "Test Form" is for? Or is there another way that I can run my form and test it that I am not seeing? Or is this functionality only available in netbeans?
I figured out how to do this so I will post this for posterity.
Jfd doesn't add a main to your project by default so adding this:
public static void main(String[] args){
GUIClassName window = new GUIClassName();
window.setVisible(true);
}
will allow you to run the module and see the window.

Console. Write text with "link" to class/line

Whenever you display a stack trace, you can get a "url-like" text which if you click it opens the appropriate class at the appropriate line.
Is there a possibility to output a text in a way that the console recognizes it and make it clickable like that?
But how would you format the output so it would recognize it as a "link" ?
You can't and you don't.
AndroidStudio supports that feature. You just need to call
exception.printStackTrace() and you should be able to click it in the console tab of IDE.
You will see something like
java.lang.Exception
at whatever.Test.main(Test.java:22)
The text inside the bracket will be highlighted and you can click it.
Based on #JEeemy's answer I managed to do this
public static void printLinkToThisLine() {
System.out.println(Thread.currentThread().getStackTrace()[3]);
}
I works for me ...
I don't know if you're using Eclipse, but the Eclipse console parses based on a pattern: FileName.java:lineNumber.
MyFile.java:3
Would link you to the 3rd line of whatever class you specified as MyFile.
You could use:
.getClass().getName()
to the the name of the file programmatically.

How to insert main method in Netbeans (shortcut)

Sometimes you would like to run a single file to test some code quickly. Typing in public static void main(String[] args) { each time is tedious. How to do it quicker?
Thanks to predefined code templates in Netbeans it's simple:
just type psvm and press Tab.
psvm is an acronym for: public static void main
"psvm" is not the most intuitive abbreviation I can think of when I want to quickly insert a main method, so I created a new one more to my liking in the Code Templates library.
"main" seemed to be more natural for me, and since there's nothing else like it in the list of templates, I used it as an abbreviation and copied the same code from "psvm" in.
Now when I type "main" + TAB (without the quotes of course) I get my main method.
It is redundant, but more intuitive for me.
To create "main" go to Tools->Options, click the "Editor" Icon, then the "Code Templates" tab.
Make sure that the "Language" combo is set to "Java"
Click the "New" button that's to the right of the "Templates" window
Enter "main" (without quotes) in the "Abbreviation" textbox that pops up
Enter the template code in the "Expanded Text" window below
my entry looks like this:
Abbreviation
main
Expanded Text
public static void main(String[] args) {$cursor}
Expanded Text (Code Window)
public static void main(String[] args) {
${cursor}
}
Of course, you can always have Netbeans create your application's main class with the main method inserted by default.
You do that by Choosing "Java Main Class" from the "New File" dialog instead of "Java Class". That will do it.
Cheers!
If you want to just run some test why not use your testing framework?
like JUnit:
#Test
public void test() {
// do something
}
This way you can even store the test for later usage.
It is properbly in most cases not a good idear to think of tests as something to execute once and then throw away.

Working with two projects-

I have developed a basic desktop-app using Netbeans IDE, on the app I have a button called Ok. Now I have created another project, its a CRUD-app. Is there a way to make the ok-button in my 1st project to run the CRUD-app in another project- or how do I link tha ok-button to open/run the crud-app? In a different project..Project names are pro1 and CruD-test-
Below is a sample-code for my button:
OK.addActionListener(new ActionListener(){
//please add a link to the CRUD-app contained in project Crud-test
#Max: I wanna run pro1 on the IDE and run the CruD-test as jar...how do I link mu OK-button to the CruD-test?
Basically it is going to be code as :
OK.addActionListener(new ActionListener(){
MyCRUDApplication crudApp = new MyCRUDApplication(); //This is only an example, keep a gloabal reference as needed
//assuming MyCRUDApplication constructor would create and show its main window.
//Otherwise you will have to provide the relevant class that really shows the project.
}
For this, you should add MyCRUDApplication application as a referenced project in Netbeans. It is explained here

Eclipse: ctrl+space completion for main no longer works

I use Eclipse (3.7.1) and I like ctrl+space autocompletion. It used to work for generating a static public void main(String[] args) method but no longer does; instead if I type mai or main and hit Ctrl-space, I get a bunch of Main classes.
What's going on, and how can I fix this?
for what it's worth, I have Lombok 0.9.3 installed, so I wonder if that's messing things up.
Had you been playing around with content assist settings?
One quick try would be going to Window->Preferences and then choosing Java->Editor->Content Assist.
On Content assist property pane press 'Restore Defaults'
or
set "Sort proposals" to "alphabetically" - this should restore default content assist behavior.
In your eclipse
Go to Windows-->Preferences --> Java --> Editor --> Content Assist --> Advanced. Here you have to make sure “Java Proposals” is ticked.
I am sure it will work,it worked for me.
You could select checkbox public static void main(String[] args) in New Java Class menu.

Categories