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?
Related
I want to run my code without creating a package in java eclipse IDE.
But when I do so it is showing error :
Must declare a named package because this compilation unit is associated to the named module 'Games'
You are using the Java Platform Module System (JPMS) which requires not to use the default/unnamed package.
In the default package delete the file module-info.java to not use JPMS and to be able to have code in the default package.
Just right click on the project folder -> New -> Package. Once the package is made, move all your classes into it.
Double click on the error icon at the line number ->move to default package option.
Make sure the classes are (public or protected) to be used in different packages.
In the file in question, right click to get the context menu. Select "Refactor->Move". The resultant dialog will have the (default package) into which the source file may be placed.
Also, be sure to not have a package definition line at the start of the source file.
Please note that the use of the default package is generally discouraged, but there are time I still use it (primarily for printing copyright/version information).
There are also options for turning on hidden/suppressed parent packages, but I think the Refactor approach is the most clean.
The most viable answer to this problem is to remove the module-info.java file located on the Project Explorer tab of Eclipse. This file is created by default. Also, deleting this will not affect your program. Hope this will help.
I'm a Java beginner and came from JGrasp where I could simply open up a class anywhere and be able to run it without any problems. When I open a class in Intellij it says the configuration isn't correct. Is there a way to set up Intellij by default to automatically set up the SDK for everything to default and allow me to open up any class without setting up configuration for each one. I attached an image that shows the edit configuration popup for a directory I opened. I am aware how to set up a project from scratch but I'd like to be able to open up code from anywhere without too much hassle setting up SDKs and configurations every time.
Here is the code :
To be able to run a class, you first need that class to have a "psvm" (public static void main, which can be easily generated for you by simply writing "psvm" when you're coding a new method in a class).
Once that's done, you head over to the left, in your project's folder-tree, right-click your class, and press the "Run" button.
The green arrow on top of the class' blue circle indicates that this class has a "psvm" and that you thus can "Run" it.
After having done this once, IntelliJ will remember that you have ran this class and the "Edit confirguration" dropdown should usually always allow you to select it. Once the class you want to Run from that dropdown is selected, you can simply press the green triangle next to it.
You should place the source code (that is file with .java extension) to a separate directory (from the compiled classes (that is file with .class extension) and set it as a source root in IDEA: right click on the folder which server as a root package directory and select -> Mark As -> Source Root.
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}
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?")
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.