how to setup "Main Class" in "Run Configurations" in Eclipse - java

In a Java project, there have two java files has main method.
The absolute paths for these two java files are:
C:\Desktop\project1\src\com\pre\moveposition1.java
And
C:\Desktop\project1\src\com\pre\moveposition2.java
When I try to setup “Main Class” parameter in “Run Configuration”, what should I setup?

Put the (fully qualified) name of the class containing main. For example, if you want to use moveposition2's main (and not moveposition1's) then you'd enter:
com.pre.moveposition2
Also, clicking on "Search..." should give you a list of classes that contain main() methods that you can choose from.

If both classes have a main() method, you can only run one at a time, since they are effectively two distinct programs.
So, in the Run Configuration, you choose either moveposition1 or moveposition2. If you later want to run the other one, just right-click on it and select Run As...->Java Application. You will now have two run configurations for your project.

Under Run Configurations, you can create multiple launch configurations under 'Java Application'. Create one with project as project1 and Main Class as com.pre.moveposition1 and try hitting Run.
You should create one more for com.pre.moveposition2 if you want to run that one.
Note: It is best practice to name classes to start with caps letters.

If both classes contain a Main() function, you should setup the class which you want your program to start with.
If only one of your classes has Main() function, setup that class.

if the method has a main method
look down the Package Explorer, select the file you want to run (that has a main)
right click it, selected Run As, select Java application.

if it's a maven project look for your class under target/classes folder. You certainly open the automatic build also.

Related

Can I create a Run Configuration in Eclipse without specifying main class?

I have multiple Java projects in Eclipse, which I would like to sometimes run with some arguments (same args for every project). Therefore I would like to create a Run Configuration which I would run on chosen project. Creating a RC for every project is a little too much hassle for me. Ideal solution for me is that I could click on a project or main class, select my run configuration and I'd run this project with specified arguments. Or maybe somebody could come up with some clever and usable alternative?
There are a few quirks, but it is doable.
Main
Project must be specified explicitly, but it is not that relevant for your use case.
Use ${java_type_name} as Main class name.
JRE
Select an appropriate JRE
Classpath
Make sure the JRE is selected in Bootstrap Entries
User entries must contain a variable string ${project_classpath}

How to add and run multiple programs in IntelliJ project?

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.

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?")

Running different classes from the same folder in eclipse

I have multiple classes inside a common package. However, the run configuration seems to default to the first class only.
I tried Right click on my class (Test3.java) -> Run as -> Java application. But event then, the first class in the package. (Class2.java) is run by default.
How can I make each individual class run when a package has many classes.
Right Clicking and "Run as Java Application" works fine for me (when the class has a compliant main method). But you can also generate multiple run configurations and pick them from the Run dropdown in the toolbar (or even save them to a folder inside the project to pin them).
Your screenshot shows, that you have started the AspectJ run type?

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.

Categories

Resources