Unable to run Java code with Intellij IDEA - java

I have just downloaded the IDE, and I want to edit my first Java file with it, I'm not interested in creating a whole project, just editing the single file.
So I opened the file from my desktop with Intellij IDEA as I set it as my default program for opening .java files.
I write some code and the main run and debug buttons are greyed out! I can't run my code!
I have already installed Java 8 update 45 64-bit (I have a 64 bit OS) as well as the Java development kit (J8U45). I have set my global IDE SDK as my JDK installation, and when it prompts me I also set this as my project SDK, but still the run and debug buttons are unable to be used!
Edit: I am also unable to run my file regardless of if its in a project or not.
Edit 2: Screenshot of my project setup

Move your code inside of the src folder. Once it's there, it'll be compiled on-the-fly every time it's saved.
IntelliJ only recognizes files in specific locations as part of the project - namely, anything inside of a blue folder is specifically considered to be source code.
Also - while I can't see all of your source code - be sure that it's proper Java syntax, with a class declared the same as the file and that it has a main method (specifically public static void main(String[] args)). IntelliJ won't run code without a main method (rather, it can't - neither it nor Java would know where to start).

My classes contained a main() method yet I was unable to see the Run option. That option was enabled once I marked a folder containing my class files as a source folder:
Right click the folder containing your source
Select Mark Directory as → Test Source Root
Some of the classes in my folder don't have a main() method, but I still see a Run option for those.

right click on the "SRC folder", select "Mark directory as:, select "Resource Root".
Then Edit the run configuration. select Run, run, edit configuration, with the plus button add an application configuration, give it a name (could be any name), and in the main class write down the full name of the main java class for example, com.example.java.MaxValues.
you might also need to check file, project structure, project settings-project, give it a folder for the compiler output, preferably a separate folder, under the java folder,

Don't forget the String[] args in your main method. Otherwise, there's no option to run your program:
public static void main(String[] args) {
}

I had the similar issue and solved it by doing the below step.
Go to "Run" menu and "Edit configuration"
Click on add(+) icon and select Application from the list.
In configuration name your Main class: name of your main class.
Working Directory : It should point till the src folder of your project. C:\Users\name\Work\ProjectName\src
This is where I had issue and after correcting this, I could see the run option for that class.

Something else that worked for me:
Right click the folder in src containing your main
You'll see an option "run 'file.main()'" with the run icon.
Click it, and then the run icon in the top right and bottom left will turn green from then on.

Sometimes, patience is key.
I had the same problem with a java project with big node_modules / .m2 directories.
The indexing was very long so I paused it and it prevented me from using Run Configurations.
So I waited for the indexing to finish and only then I was able to run my main class.

If you can't run your correct program and you try all other answers.Click on Edit Configuration and just do following steps-:
Click on add icon and select Application from the list.
In configuration name your Main class: as your main class name.
Set working directory to your project directory.
Others: leave them default and click on apply.
Now you can run your program.enter image description here

Last resort option when nothing else seems to work: close and reopen IntelliJ.
My issue was with reverting a Git commit, which happened to change the Java SDK configured for the Project to a no longer installed version of the JDK. But fixing that back still didn't allow me to run the program. Restarting IntelliJ fixed this

-First Move Your Code Files in side the "src" Folder
-Make sure your Main method is declared like the following
public class Main {
public static void main(String []args){
}
}
then:
Go to Project configurations
select Java application,
check allow parallel run
and select your main class
and it should work

If you are just opened a new java project then create a new folder src/ in the man project location.
Then cut and paste all your package in that folder.
Then Right click on src directory and select option Mark Directory As > Sources Root.

If you use Maven, you must to declare your source and test folder in project directory.
For these, click F4 on Intellij Idea and Open Project Structure. Select your project name on the left panel and Mark As your "Source" and "Test" directory.

Related

How to create a executable jar with a particular class in a project in netbeans

This is my first question on stack overflow.
Please have a look at this first, please!!
I am creating an executable jar file by clicking on the button surrounded with grey color. The problem is that the exxcution of the application always starts from the "PersonalAssistant.java".
I want my application to begin its execution from "App.java" which also contains a main() method.
So, basically the jar file which I want should begin its execution from App.java class which has a main method.
I don't know how to change the path or execution class for jar.
Please help me!!!! I'm stuck with this silly problem. But I want a solution for this.
I'm using Windows10 with Netbeans 8.2 IDE
You should be able to reconfigure your IDE and rebuild the executable jar specifying which class you want to use as the executable.
in your IDE add java -cp myjar.jar App
to your compile command
this might also help
Look at the picture above. Right-click on your project and select Properties. Select Run. Look for the Application Class field. In my example, I have javafxapplication211.Main. This structure is projectName.MainClass. You should just need to change the MainClass part to the class you want to start your project. For example, if Main.java was starting my app and I wanted to change it to App.java. I would change javafxapplication211.Main to javafxapplication211.App.
If you hit Browse, it will give you a list of available classes that can start the application.

Use Java class file in IDEA

Is it possible to use a class from a .class file in IDEA? For example, if I have a directory with two files Ball.java and DemoBall.class, how would I use DemoBall in Ball? It says it cannot find symbol when compiling. I just want to tell IDEA that there is a compiled Java class in the directory and to recognize that, I don't want to decompile it.
EDIT:
Open IntelliJIDEA. Now Press CTL + Shift + ALT + S. This will open Project Structure.
Now Select Module from Project Settings (left hand side). And Dependencies tab from the right hand side.
Now Click on the + on the extreme right hand side toolbar. Select Jars or Directories. A new Window, Attack File or Directories will pop up.
Now browse to the location, where DemoBall.class is located. Click OK.
Select the CheckBox under Export against the location just selected. Click OK on the main Project Structure window. You are done.
Original(This one it seems is not the right way):
Open IntelliJDEA. Create a Project, with simple Java Class named Ball.java
inside it, having one main method(empty body).
Run this class, so as to create one out folder.
Copy DemoBall.class. Now through Windows Explorer/File System, go to out folder and paste DemoBall.class inside out\production\ProjectName folder.
Now simply write the code that uses this DemoBall class, inside the main method of Ball.java class.
Previously, I was wondering, as to why the red lines are coming still, even though, the program is running fine. But once, I restart, IntelliJIDEA, everythingy works fine, without showing any errors.

Eclipse - Source not found

I know this question has been asked many times before, but none of the proposed solutions resolve my issue (or I'm not implementing them correctly).
I'm developing a plugin for OpenFire, and when I set a breakpoint in my plugin source Eclipse reports "Source not found". The JAR is built separate from the build of the OpenFire server. I've tried adding the JAR and specifying the source code directory, but no dice.
Here's the process I'm following: When I hit my breakpoint I'm clicking "Edit Source Lookup Path", clicking "Add", Java Library, User Library, click User Libraries, adding my library (tried both "Add JARs" and "Add External JARs"), and then specifying the folder that contains the source code. I've also tried:
Adding the source by selecting "File System Directory" instead of "Java Library"
Adding it by selecting "Workspace Folder"
In the Project Explorer, adding the source for my plugin to the Java Build Path
In the Project Explorer, adding my JAR as a Library and specifying the Source attachment
Thanks in advance for any suggestions.
Personally, I have no good experience in Eclipse JDT, when adding a source folder at the time it hits a break point. What I always prefer to do, is adding the source folder before debugging:
Select Project/Properties/Libraries
Select your library and "edit..." the source attachment
Select either the corresponding source folder or the zip/jar containing the package
After that, you should be able to open the source files in the virtual "Referenced Libraries" folder (directly below the "JRE System Library" folder inside the project), or when opening a reference to a linked class file from within the JDT editor. If you are able to access the source, then you should also be able to at least stop at the break point and see the corresponding source.
Else, you will have to check again, if the source folder is really valid: The source folder or source archive must contain the folder with the name of the root package of the library (e.g. the default Java "src.zip" inside the JDK folder also includes a corresponding "java" folder at its root). When in doubt, extract the archive and select the parent folder of the package you are interested in - sometimes source archives might be a mess or incompatible to JDT.
If this has been assured and it still doesn't work, chances are, that your linked source folder does not correspond to the compiled version of the library. Usually JDT will handle such inconsistencies fine, but if you try to open a source file, that is entirely different than the corresponding class file, you will encounter problems. In this case I would suggest either downloading the correct source version of the library, or recompiling the library from the source, if all else fails.
If opening a referenced source file from withing the JDT editor does work fine, and you are still unable to open the source files when a break point is entered during debugging, then most likely the class files are missing the line numbers of the corresponding source file. Again, you will have to recompile the library from the source in this case.
Finally, it is also possible to overwrite the default source lookup by specifying source folders or archives in the source tab of the launch configuration. But you should normally not need to do this, when your build path is configured correctly. From the Eclipse Help:
The Source tab defines the location of source files used to display source when debugging a Java application. By default, these settings are derived from the associated project's build path. You may override these settings here.
I had this very annoying problem for a long time but was finally able to solve it. In my case, a null pointer exception was being thrown somewhere in Java's Transformer.IsRuntimeCode(ProtectionDomain) function.
I didn't really need to know about this since the exception was being caught and handled, but eclipse would pause debugging every time this happened and tell me that the source wasn't available. As a result, I constantly had to keep pressing the button to continue code execution.
In order to prevent this from happening, I:
1. Clicked on the "Breakpoints" window at the bottom of the debugging
screen
2. Right clicked "NullPointerException"
3. Unchecked "Caught"
This prevented the debugger from pausing program flow during a caught NullPointerException.alt text
(source: SharpDetail.com)
And this another one as:
Eclipse doesn't crash. You're trying to step into a method, where eclipse doesn't know where to find the source (*.java) files and informs you about this. Here is how to tell eclipse where to look. Go to
Window->Preferences->Java->Installed JREs,select the JRE you are using and click Edit.
There, select all of the jar files in the list you see and and click Source Attachment....
In the window that shows up, select the fille src.zip, which is in your JDK folder (if you didn't uncheck it while installing the JDK). On the machine I'm on right now, that is
C:\Program Files\Java\jdk1.7.0_07\src.zip.
Save all your changes (possibly restart eclipse) and you won't see that error again.
Well it turns out the solution was anticlimactic. When Balder's recommendations did not work I tried debugging one of the stock OpenFire plugins and it worked just fine. I then created a new plugin from scratch, and by doing nothing other than adding the source to the project (Right-click on the project -> New -> Source Folder) it also worked just fine. I have no idea why Eclipse refuses to see the source for my original plugin, but I moved all my code and libraries over to the new plugin and debugging is working as expected.
Many times you put jar files in eclipse IDE as referenced libraries when binary version of application was downloaded. Usually done so by configuring the build path. But the binary does not have the source files. You have Binary version and source version of application.
One simple way is to download the source libraries that you also use maven to build the project.
Keep somehwere, may be inside your project workspace.
Now while seeing the class file (from eclipse, jar exploded) you may see the source not found, fine.... there is a button below and click on that, a new window opens and there select add external folder.
Reference it to the src folder of the source you downloaded(not the binary one) and kept somewhere as said above and it will show the class details from that.
I fixed this issue with doing the following:
Click at the menu Window - Preferences - Debug - Step Filtering And check all the packages like the following image.
(Step Filtering)
Then, debug again your project and thats it.
Best regards,
In my case, I had a breakpoint in the class declaration. I mean in the next line.
public class GenerateInterface implements JavaCall {
So, the debugger stopped in that line and showed the following message:
Source not found
I think unconsciously activate the breakpoint in that line.

Eclipse will not run programs or detect errors

My eclipse seems to be pretty screwed on my laptop. Whenever I load a program up from College, it does not detect the errors or anything. Also now when I attempt to run the program, it won't allow it; i'm given the message
unable to find an ant file to run
Anyone able to help me out?
1-Create a new project in Eclipse
2-After the project is created, look in the package explorer window pane on the left and right click on the src folder.
3-There are two methods for the next step, you could either add a New > Class, and then copy and paste everything from your old java file to the new class (make sure the class name is the same), or the better route would be to Import.
4-After clicking Import, select File System under the general folder. Click Next.
5-Browse for the java folder where your source files are located. Once you click ok, it will add all of the source files to the right pane.
6-Select which files you want to add and click Finish.
7-Now if you look at your Package Explorer window you should see the source files. Now just
compile them, and the error shouldn't appear and you can run them just like before.
By creating the new project, you're making sure Eclipse knows where your source files are located (in ./src) so that it can compile your code in that location.

Eclipse Error: Could not find or load main class [duplicate]

This question already has answers here:
Eclipse "Error: Could not find or load main class"
(61 answers)
Closed 9 years ago.
Have Googled extensively on this error, but I can't seem to fix the problem. I've written a basic java program in Eclipse Juno, as follows:
public class HelloWorld {
/**
* #param args
*/
public static void main(String[] args) {
System.out.println("Hello Eclipse!");
}
}
After clicking Run, I get: "Error: Could not find or load main class HelloWorld". This error message applies to all my other projects in the same workspace. I've tried switching workspaces but the error still appears.
I'm on Windows 7 64-bit. Any help would be appreciated!
This just happened to me today after updating my JRE. I cleaned the project and it started working again.
Project -> Clean will remove any existing class files and completely rebuild the project. There's more information on Eclipse's clean function here.
I just ran into that problem. The cause... not sure. It only happened to me after I added a new JVM.
My solution:
went to run configurations: - run->run configurations
In the Classpath tab:
Select Advanced
Add where Eclipse usually put the *.class for the projects, which is in bin. So I added the bin directory for the project.
I deleted a jar file from the bin directory. Right click on your project - Properties then Libraries tab. There was a red flag in there. I removed the jar file from the Libraries and it worked.
It seems that the class is not compiled by Eclipse.
Few pointers could be-
Check if the .class file exists in your output folder.To know your output folder Right Click on Project->Properties->Java Build Path(Check at bottom).
Check if Project->build Automatically is checked in the menu.
Check if the HelloWorld class is in src folder or not.Right Click on Project->Properties->Java Build Path(Check source tab).
Same Problem occur with me.I went to Project > Properties > Java BuildPath.
There In order of export , I moved up my java/main to the top priority.
that's because you guys created the class one time with the main method & after that may be you have been deleted that form or workplace & still some of the files exist ,i will suggest you to create that form or workspace again & then delete it by clicking on it completely,then after that if you created the some class like Runner class try to run it again.
This happened to me. I noticed that someone said I have to create an entire new WORKSHOP! Why? Because I installed a newer JRE version and that won't allow other previous versions to run on it. So all those old files I have become useless in a way. Not really, Just copy and paste it to new class and change it to an unused class name.
if your package name is same with your class name this problem will occur.

Categories