How to add and run multiple programs in IntelliJ project? - java

I'm trying to add another class to an IntelliJ project I created. I already have one program in the project and it is running perfectly. I tried to add another program to the same project's src folder but cannot Run this file. The s/w allows me to build and compile the file but I'm only able to Run the first program I created in the project.
I could do this in Pycharm but not in IntelliJ. Am I missing something? Is there a different method to achieve this in IntelliJ?

You should press shortcut :Alt+Shift+F10.Then you could choose which program you wanna run.

If you click on main method not found, it should bring you to the page where you can add the configuration.

Related

Running Java file in IntelliJ

I need some help with IntelliJ. I am fairly new to programming, and I have some experience with the IDE but I am by no means a know-it-all when it comes to IntelliJ.
I have 3 Java files (without their respective class files). When I open intelliJ it asks me:
Create New Project
Import Project
Open
Check out from Version Control
I click on open, IntelliJ opens up a file location, I click on one of the three Java files to open, and I drag the other two one by one (they open in their separate tab).
I cannot figure out how to run them. It is likely that I need to create run configuration and I don't know how to do it. I know how to create a new project and get everything working, but I don't know how I can get the files working if I just open them. Can someone tell me how to create the run configurations to run the files? Please and thank you.
IntelliJ IDEA is a great IDE!
This is what I would do:
Select File/New/Project... in the menu. You will get a new modal window.
Then select Java on the left, make sure that you have your JDK selected (top right), it not there, create it right from the window. Click 'Next'.
Just go 'Next' on the 'create project from template' step.
Then type your project name and select a folder for it. It will create a new project.
After that, copy your java files into /src folder using drag-and-drop in IDEA or just a file manager you like.
Now you have an IDEA project with your source code in it. You might need to sync your IDEA project, use File/Synchronize for that.
Finally, you can right click on you java file and 'Run' it from IDEA.
It the project fails on compilation you might need to add required imports and libraries, but that depends on your code. IDEA will highlight all the errors.

Changing which java file runs first when opening jar

I'm not entirely sure how to word this but how would I go about changing which java file in my package is the first one to open when the compiled jar file is executed?
I'm also using netbeans if that helps.
You can changed it in Netbeans. When you run the whole project in netbeans, the first class that is executed there is also the first class to be executed in the jar file built by Netbeans.
You can right click your project in the project window and select Properties. From the window that opens, select the run category in the left side. Then on the right side, there is a button to browse and select the file for the Main Class.
The manifest indicates the Main-Class that runs when you execute a jar file.
https://docs.oracle.com/javase/tutorial/deployment/jar/appman.html
If you have two (or more) classes with public static void main, click File | Project properties and in the Categories dialog, click Run and look for Main Class:. and then click Browse... to select which main class to start with.
(But I'm not sure this answers what you really want to do since once compiled with the chosen main class, there's no way to later have the .jar file run another class. If this doesn't answer your question, maybe a good question in return is, "What are you trying to do?")

Eclipse cannot create runnable jar - No resources selected

When I try to export my project as a runnable jar Eclise gives me the following error:
No resources selected.
The project runs fine within Eclipse, I already tried to clean the project before exporting, but that didnĀ“t work.
Any ideas on how to fix this.
The problem is most likely with your run configuration.
Go to your Class that contains the main(String[] args) method you wish to automatically run
when the jar is called from the command line
Right-click->Run As...->Application
Make sure it runs to your satisfaction
Now go through the export process as before, selecting the newly created launch configuration.
The last step is key.
I found another solution.
I found that there's a bug in JDK 9.
I solved it by downgrade to JDK 8(1.8.1)
I fixed the problem, I exported the project as an archive file. Then I opened up a new workspace and imported the the archived project. After that I tried to export the project as a runnable jar, after two tries the jar runs as is it should.
I still don't know what the exact cause was but I hope someone how has run into the same problem finds this helpful.
Doing a (project->clean) and creating a new run configuration (with correct project and class name with main() method, selected) solved the problem for me.You just have to select the new run configuration while exporting runnable jar :)
Try this, this is the way to create a jar or runnable jar in eclipse, all your external libraries in the project will be included
File -> Export-> Java ->Runnbale JAR file
Launch configuration : your Class containing the public static void main(String[] args)
Export destination : Target place
Library Handling:
Package required libraries into generated JAR
FINISH
When you click this option in Eclipse you should get a popup that lets you choose what files to export, you need to select the src folder.
I was having this same problem, and I was able to resolve it. Maybe you should cross verify the main class, that you selected in this project. It is possible that some other project's main class may have the same name as the main class you selected. Make sure the main class you selected is the one from the project that you selected.
The problem is your package. You probably renamed it, and in the export option, the program was confused, and it was using the first name of your package.
This is basically happen when you change package name under which you made your program. This will create confusion regarding package, as per program export, main method still written in old package.
To resolve it, Just copy it and paste it in same package with different name and then try to export it. Check for log too.
I was in the same situation. For me deleting existing eclipse installation(not not the project workspace) and opening the existing eclipse workspace with new installation of eclipse was the only thing that worked.
Just copy the project as a new one. Configure the build path by creating new Run/Debug Settings.this would resolve the problem.
I continued to get the "no resources selected" error. I then did the procedure I am quoting and it gave me a MANIFEST file. Then I was able to create an executable JAR.
Quoting: "I exported the project as an archive file. Then I opened up a new workspace and imported the the archived project. After that I tried to export the project as a runnable jar, after two tries the jar runs as is it should."
I also faced the same issue, in my case, I just closed the project and reopened it, now it's wokring
Check that your project is correct at the time when you select the main class.
If the class non exist, it's the standard error of Eclipse.

Subclipse problem: running .java file as Java application

After checking out code for the first time from a repository into Eclipse using the Subclipse plugin, I've found that I am not able to run Java applications anymore. Usually when I right click in the text editor window and select "Run As", I see the option to "Run as Java Application". Now, however, all I see is "Run on server." Is there another way to run it as a Java application or is there some option that I need to re-configure?
Does the class you're trying to run have a public static void main(String[] args) method signature?
I actually figured it out - I checked out the wrong directory. Because the src directory was a sub-directory, it wasn't being recognized as a package and thus wasn't allowing the .java files to be run as Java apps. Once I checked out the right directory, it worked.
You can go to Run As->Run configurations to set where your main class is located, as this was likely changed or not set.
I have seen this if the Eclipse project is not a Java project.
Does it have Java nature?
If not, delete it, and use Check out as.. to ensure it is setup correctly. This behaviour depends on the existance of the .project and .classpath files in the source repository.

How to reuse eclipse launch file in a new project

I just checked out an svn project in eclipse as a java project. There is are couple of run launch files in it that i want to reuse but dont know how to point 'Run' to see those launch files or use it. Any tip? thanks
Usually these are automatically picked up by the project and placed in the run menu. If they are not, Right click on the .launch file then choose 'Run As' and select the first option. This will run using that run configuration.

Categories

Resources