Running Projects in IntelliJ - java

I've got a project from my teacher, in which i should add my implementation for a certain aspect.
The Project already got a main method but still the "Run" and "Debug" buttons in the top right corner are greyed out.
I've also tried right clicking the main method and java file, but it doesnt show me run or debug.
Also tried using the "Make Project" Button (top right corner), but it doesnt let me run it either.
How can i run the project?
Help would be much appreciated cause I cant seem to find a solution...

the "run" and "debug"-button in the top run the currently selected main-method. As long as you haven't selected one, you can't run it. In the current version on the left side of the editor-pane, there should be a green arrow right next to the main-button. Run it once via clicking that arrow and from that point on you can run/debug the program using the buttons on the top-window.
In this image you can see the green arrows (version 2016.2.4).
Alternatively, you can use the following more complicated, but also more versatile option:
Directly from the run- and debug-buttons is a button that toggles a list. In the initial configuration of the project, this list contains exactly one Item: "Edit Configurations...". Use it to create a custom-target - go for Application, if your using a java-class as entry-point - and set up the configuration as you wish. Clicking the arrow mentioned earlier does basically the same with a bit of automatic configuration.
Third alternative:
Open the context-menu on the main-method. It as well contains options to run and debug the program with the marked method as entry-point. Works for the context-menu of the respective class as well.

You have to add a run configuration. Do this with clicking the grey button and then clicking at sth with configurations (i am not in front of my PC), then add there the config

Does your main method have the proper syntax for arguments? I found that in Eclipse, the program will not let you run the main method if it is not in the format of public static void main(String[] args). Also, does your code compile properly and your IDE just doesn't let you run it?

Related

Eclipse isn't showing anything

The screenshot of my starting eclipse page.
So I'm very new to eclipse. I'm 16 and need it for school, I've been using it for 2 months now.
One of my assignments was to use JFrame and create a GUI which I had to import into eclipse.
However, when I created the java project and then tried to use JFrame it would show me the source(I think that's what its called) but wouldn't let me open the design part.
So then I tried to create a class in a new java project and it wouldn't show me any code so I deleted eclipse and tried to install it again.
Which I did twice and now I just get this screen whenever I open it. I can't find the project explorer and I cant use it.
Please help me to fix and get the screen back to normal with the console and terminal and package explorer visible as well as how to use JFrame properly.
Click Window > Show View > Project Explorer as shown below:
Similarly, open other views like Console, Problems, Outline etc. (whatever you need).

Eclipse problem : the selection can not be launched, and there are no recent launches

I am a newbie with Java and trying to write a very simple program like this ofcourse
public class FirstProg {
public static void main( String[] args) {
System.out.println(" I am learning how to code");
System.out.println("Today's date is");
}}
When I ran it, it said to me that, the selection can not be launched, and there are no recent launches , I ran it on Eclipse. Could you please give me some ideas? I am a newbie and very confuse with this problem. Thank you very much.
It is difficult to be exact, because we don't know exactly what you've clicked on, or what version of eclipse you have. Eclipse is very flexible, and one result of that is that it does not have the same subwindows and buttons for every user.
There is a standard 'view', i.e., a subwindow within Eclipse's main window, called "Project Explorer. It is most often on the left side of the eclipse window, and displays folders and files in one or more projects.
Find the folder for your project in that view, right-click on it, and look for the options "Run As..." and "Debug As...". Those are also menus, they will have options that depend on how your eclipse is configured. But one of the options should be "Java Application", which is the right one for what you're doing. "Run As..." runs your program; "Debug As" runs it under the eclipse debugger.
If there is only one Java class in the project with a main(String[] args) method, then eclipse will run that class. If there's more than one such class, it will ask you to choose.
Once you've done this, there are usually buttons on the top of eclipse; one is an insect-looking icon, the the other a triangle with a point to the right -- the first is 'debug', the second is 'run', and once you have debugged or run a class, clicking on one of those will debug or run the last class you debugged or ran.
Good luck.

Intellij auto-generate java method at the end of the file

Is there any way to ask the auto-generate methods created by intellij to be generated at the end of the class?
currently when i generate a method (⌥⌘M) it is created right below the function I extracted the method from, and I would like it to be at the end of the file.
Does intellij offer that option?
You can re-arrange the code in IntelliJ after code generation manually even though this does not guarantee that the code will go to the bottom of the file. You can do that by clicking on the "Rearrange" menu item in the "Code" menu:
Another option to re-arrange the code without having to use the menus would be via the "Reformat" file dialogue, by pressing on Windows Ctrl+Shift+Alt+L and by activating in it the "Re-format code" checkbox.
You can change the pattern of code arrangement in the settings in the following dialogue:
There is no such option in Intellij, Once you extract method you can move method up and down using Press Up( ⌥ ⇧ Up) or Down( ⌥ ⇧ Down).

Is there a way to use breakpoint in eclipse android, the same we use breakpoint in Visual Studio or Netbeans?

I have tried to use breakpoint in the same way we use break point in Visual Studio or Netbeans, we simply mark the break point and when statement executes, the breakpoint highlights and we can see the values in respective data structure or variable using breakpoint explore.
As far as i have used eclipse, i have found break point is there too, but it is not in that way, means on runtime it does not pick the respective point and neither i can see values nor the application works perfectly while doing that. Is it not possible in eclipse or some tool is required, or i am wrong, please guide.
Ctrl+Shift+B while on the line.
Double click on the left most bar in Editor View.
Right click > Toggle Breakpoints on the left most bar in Editor View.
put a breakpoint at you code line you want,
then right click on project-> debug as -> android application
then go to window menu -> show view -> other... -> debug
or as show bellow
You must set a breakpoint and run the application in debug mode.
You have to launch application in Debug Mode. You can find Debug button left to Run button. It has bug as an icon.
Also could be useful: Setting up a Device for Development. It describes how to enable debugging on your device

Netbeans - Error: Could not find or load main class

So I'm making a java application in Neatbeans 7.4, been working at it for a while, everything was fine, running the project worked fine, but now when I hit run project, I get the error
Error: Could not find or load main class phleveledit.MainWindow
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
I can't think of what I did right before this started happening, so I don't know what I should change.. The code doesn't appear to have any errors. Here's a screenshot of the IDE+project folder
Image (http://puu.sh/5ldYB) :
Any ideas?
Edit: Unfortunately I happened to fix this problem by removing some code, which probably had some errors Netbeans couldn't detect, but I don't know what was exactly wrong so can't vote on a correct answer.
Right click on your Project in the project explorer
Click on properties
Click on Run
Make sure your Main Class is the one you want to be the entry point. (Make sure to use the fully qualified name i.e. mypackage.MyClass)
Click OK.
Clean an build your project
Run Project :)
If you just want to run the file, right click on the class from the package explorer, and click Run File, or (Alt + R, F), or (Shift + F6)
Just close the Netbeans. Go to C:\Users\YOUR_PC_NAME\AppData\Local\Netbeans and delete the Cache folder. The open the Netbeans again and run the project.
It works like magic for me.
(AppData folder might be hidden probably, if so, you need to make it appear in Folder Options).
You can :
RightClick on project node and go to Set configuration
Select the main class for your application.
Then clean and build.
Even if the above steps don't work for you then then delete the Netbeans cache by deleting the (index) folder
User\.netbeans\SOME_NUMBER_reflecting_your_version\var\cache\index\
Sometimes due to out of memory space error, NetBeans does not load or find main class.
If you have tried setting the properties and still it is not working then try
Select the project from the project explorer
Click on Run in the Menu Bar
Click on Compile
It worked for me.
This condition happens to me every 6-months or so. I think it happens when closing NetBeans under very low memory conditions. I discovered that it could be easily corrected by (1) Rename your project, including its folder name using right-click on project explorer's project name---I put a simple suffix on the original name ("_damaged"). (2) Try BUILD. If that is successful, which it is for me, give three cheers. (3) Repeat step (1) to restore the original project name. BUILD and RUN should start without trouble.
I guess that the 'rename the project and folder' process causes a special rediscovery of the applications main location.
Try to rename the package name and the class/jframe names... The clean and build the application.
Right Click on the package name
Go to Refactor
Select Rename
Give it a meaningful name, preferably all in small letters
Click on Refactor
Do the same for the class/jframe names.
Last Select Run from Menu
7.Select Clean and build main project
That should do it!!! All best
I had the same issue once. The problem was not in the code. The cause was... renaming the project folder to some other non supporting name. My project name was "MobStick" and I renamed it to "MobStick - May 26, 2014 04:00PM". Renaming it back to normal solved my problem.
I have run into this error a couple of times as well and for me the above solutions did not work. What does seem to work is going to the Project Properties, and under Compiling toggling Compile on Save.
Using NetBeans 8.1, I got the dread
Error: Could not find or load main class
from carelessly leaving an empty line in the Project Properties > Run > VM Options field. Until you click in the field, you may not see the caret flashing out of place. Remove the empty line to restore equanimity.
I just ran into this problem. I was running my source from the command line and kept getting the same error. It turns out that I needed to remove the package name from my source code and then the command line compiler was happy.
The solutions above didn't work for me so maybe this will work for someone else with a similar problem.
I had the same problem, I had the package and class named the same. I renamed the class, then clean and build. Then I set the main class in the "run" under the properties of the project. I works now.
I found the following steps useful:
Right-click on the project in the left toolbar.
Hover over the 'Set Configuration' item.
Click on 'Customize...'
Click on 'Browse...' by the 'Main Class:' item.
Select the correct class.
Click 'Select Main Class'.
Click 'OK'.
My problem was that, apparently, my package name was being listed twice. Selecting the class using the dialog changed 'aclass.MainClass' to just 'MainClass'.
Hope this helps,
-HewwoCraziness
Edit: This is expanding on Mary Martinez's answer.
You can solve it in these steps
Right-click on the project in the left toolbar.
Click on properties.
Click on Run
Click the browse button on the right side.(select your main class)
Click ok
Possible Fixes:
Fix 1
Go to project properties (right click on the folder of your project in netbeans)
On left tab where it shows the categories, click on the "Run" selection
Then click on Browse to find the Main class you use on your project
Fix 2
Go to C:\Users\name\AppData\Local\Netbeans
delete the Cache folder.
Rebuild and Run
Fix 3
Download most recent version of Netbeans
Fix 4
Download most recent version of JDK and configure Netbeans to use that
I had the same problem for 3,4 days. On my PC my Jar file snapshot would give me this error while on my laptop it would work fine, I tried all the tricks shown above and on other forums like deleting cache, selecting main project file, etc, but somehow I was sure the reason it cannot find the main class when I would execute the JAR file was may be due to classpath issue in maven configuration, and I was right and I fixed it using following steps:
Right-click on the project, and go to the properties
Inside the properties go to Actions
On the right side in Actions select "Run Project" and you will see properties below
Inside "Set Properties" make sure exec.args=classpath %classpath "package_name"
In my case, the package name was accompanied by the main class. So my main class was Login while the package name was com.mycompany.islamic_center_app1, When I checked the entry was
com.mycompany.islamic_center_app1.Login
All I did was remove ".Login" from com.mycompany.islamic_center_app1 and it was fixed, no more errors.
close netbeans.
open netbeans again.
choose new project>>java application.
click next.
deselect create main class.
now make the application
clean build run
For more reference watch this video
try this it work out for me perfectly
go to project and right click on your java file at the right corner,
go to properties,
go to run,
go to browse, and then
select Main class.
now you can run your program again.
I had the same problem and I moved the project to a location where the path had no none-english letter and that fixed the problem
if you are on window os, then try to start NetBeans via administrative mode. right click on NetBeans icon and "Run as Administrative".
If none of the above works (Setting Main class, Clean and Build, deleting the cache) and you have a Maven project, try:
mvn clean install
on the command line.
Had the same problem here. Usually Clean and Build solves much of the problem. It happened to be caused by a wrongly installed plugin.
I faced the similar issue with Netbeans 10 and JDK 1.8.
I was not able to choose the right class to launch the project
When I compile or run the project, it shows me the Class name as "initializing view, please wait ...", I could not select the class name.
The issue was resolved with the NetBeans11.3, I am able to choose the correct Class file without any other changes, and the project is launched without any issues.
I had the same issue but none of this thread's solutions worked for me. Finally, it was OneDrive that caused the issue (for once more). So, I simply moved the NetBeansProjects folder from Documents which is synced with OneDrive, to C:\Users\yourName\AppData\Local\NetBeans (selected this path as there is already a NetBeans folder) and that was it, case closed.
If you also have NetBeansProjects to a OneDrive syncing folder it is worth trying this solution, just be sure that the path you will select is not synced with OneDrive. Also, remember to close Netbeans before making the folder change and after you move the folder to the new path you need just to open Netbeans again, go to file menu/open project and select your project from the new path.

Categories