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.
Related
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.
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.
Does anyone know a reason why my Eclipse does not preload
public static void main(String[] args)
when I create a new class? What can I do to make it appear automatically?
Type main and press ctrl+space. The eclipse content assist will pop up with main method. Press enter.
"what can i do to make it appear automatically? "
Go to create New Java Class dialog
See a part that says "Which method stubs would you like to create?"
check public static void main(String[] args)
I think the best practise is to use only keyboard. It makes workflow a lot faster.
Create new java class with Ctrl+n
on dialog box, enter its (class) name
use (left)Alt+v to easily tick checkbox for - public static void main(String[] args)
then press [enter] Finish and voila class is ready...
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.
I have created a new DesktopApplication in Netbeans. When I start it, it shows the gui directly on the screen. How to hide it after startup?
Something like this:
DesktopApplication1.getApplication().getMainFrame().setVisible(false);
after the initComponents(); method doesn't work.
Is there a way to hide this window after starting up? (I only want to show it after clicking the tray-Icon of this application. Not after startup.)
Thanks.
This problem is reproduceable when you create a new DesktopApplication in Netbeans. I haven't changed the code (only added the line mentioned above.)
If you look at the source code for DesktopApplication1App, it says something like
//DesktopApplication1App.java
#Action public void startup(){
show(new DesktopApplication1View(this));
}
To fix this, just comment out the show() call, and replacing it with a dummy. For example:
//DesktopApplication1App.java
#Action public void startup(){
Object o = new DesktopApplication1View(this);
}
Later, if you want to set it to be visible, you can call this:
//DesktopApplication1View.java
DesktopApplication1App.getApplication().show(this);
// ----- OR -----
this.getFrame().setVisible(true);
whichever works for you.