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

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}

Related

I'd like to import class for another project in Java

(Thank you #MarkRotteveel !)
I'm learning Java. I want to import class of another project. 'a' class('MySoup') in project A('recipe_nomodule') to project B('test')
However, there is no JRE System Library and it doesn't work. And I can't find posts about this problem
ㄴ 1. I created another project. (recipe_nomodule)
and a package(recipe_nomodule) and also a class(MySoup)
ㄴ 2. I created recipe.jar
ㄴ 3. Java Build Path - Libraries - Add External JARs... (in Properties)
ㄴ 4. result.
"MySoup cannot be resolved to a type"
"The type recipe_nomodule.MySoup is not accessible"
(23/01/2023 Thank you for the answers)
In the 'build path' settings of your new project (as per the screenshot you posted for item 3), add the other project in the 'project...' tab.
You don't need to make a jar, you don't need to add that jar via 'external libraries..'. You don't want to do that - you want the project dependency. That way, you can edit a file in either project, just save the file, and run, and see the updates, without having to go through the routine of making another jar. In debug mode, any change that doesn't affect signatures will even be instantly applied, no need to restart (only applies to long-running apps, of course).
Why doesn't your current approach work? Your question doesn't include enough information to be sure. A few options, based on the fact that key error message is 'not accessible':
You've explicitly told eclipse you don't want the package recipe_nomodule to be considered accessible by anything except the project that created it. I doubt you've done this.
It's set up as a module-based jar (with a module-info file), in which case, you'd have to export the package in the module-info.java file.
The class wasn't marked public at the time you made the jar file, even if it is now (this gets back to: Don't go via jar files, that way any updates won't propagate and that's very annoying).
More generally if you want to ship these concepts completely separately, you need to start answering questions about how you'd like your jar file to be distributed, you need to sign up with sonatype/mavencentral or some other entity that distributes open source, you need to learn maven, and more. Probably not worth getting into that, especially given that 'for fun' / 'for exercise' projects wouldn't be accepted.

Run mvn package every time I make changes?

Working on a DropWizard project for the first time which uses the docker-config.yml file to run the server using the jar file.
The command I run on the terminal to run the server is something like this:
java -jar target/foo-0.0.1.jar server conf/docker-conf.yml
Do I have to run mvn package every single time I make a change? (for eg: even a small change like adding a System.out.println() statement to debug something)
mvn package is used to create a JAR - a deployable artifact of the application. So if you really want to create the JAR every time, yes you need to package everything.
However, usually if you’re talking about small changes like adding System.out.println I assume that you have some “work-in-progress”. In this case you can configure your IDE to run the class with main method (the Application class in in terms of dropwizard framework). It won’t create a fat jar but for debugging you also don’t really need it.
This is what I (and I believe many of our colleagues) do when working with applications powered by dropwizard, spring-boot (which is similar in this aspect), etc.

IntelliJ doesn't pick up generated .class files

I have implemented an AnnotationProcessor that picks up class annotations that take a string argument. The string argument is an expression in a domain-specific language, which the annotation processor will use to compile a class file.
I create a small test project to try this out. The behaviour I see is this:
I can successfully build the project using maven
I can successfully run the project from within intellij
Despite the project RUNNING in intellij, the generated class is not recognised in the editor ("Cannot resolve class '...'"), and intelli-sense does not work, either.
I've tried to find the issue and found:
the class file that is being generated is being created in target/classes/package/name/KlassName.class (this is the location that the Filer::createClassFile method picks, I'd have expected this to go to some separate directory though).
if I'd create a java source file during annotation processing (using Filer::createSourceFile), intellij would have no problem. However, I can't do that, since the compiler is a library that really must create classes directly.
I have two guesses about what a solution might look like:
This problem might stem from intellij not looking inside target/classes when type checking in the editor window.
The class files should be generated in a separate directory instead. If so, what is the setting to fix that?
I have reproduced this issue using intellij IDEA 2016.2.1 and intellij IDEA 2017.2 EAP.

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

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.

Eclipse: Get the project name as a VM argument?

Consider a configuration class which needs to parse a different config file for each Eclipse project.
For instance, If Configuration class is called from Project1, it should parse special/path/fileX, and if it is being called from Project2, it should parse special/path/fileY.
I've tried using Eclipse's {project_name}, but surprisingly it is being parsed to the project being highlighted by the cursor, not the parent project of the current class.
Any ideas how can I distinguish one project from another in Eclipse Runtime, preferably using JVM arguments?
This is a workaround to your described problem, but I guess it's a better solution as it's independent from Eclipse and will easily work in production too.
Simply load the same file from the classpath, regardless which Project you're starting this from:
/my/file/from/classpath.txt
But put this file only in Project1 / Project2, such that the various files mutually exclude themselves according to your set-up:
/Project1/src/my/file/from/classpath.txt
/Project2/src/my/file/from/classpath.txt
Where src are Java source folders. Now you can still use your JVM parameter to override this, or to provide a default if the above is missing.
I think the easiest way would be using eclipse's run configurations. Simply create as many configurations as you want, supply all the JVM args you want, and use them to launch. This will also be very close to what you're going to do in production mode
or you can go to your Eclipse' Installed JRE's and have the configuration set as JVMArgs just the way you would have in prod.
If required, you can have diff copy of JREs per env.
Also if running from the IDE, you can get the
System.getProperty("user.dir")
which should be the root of the project.
that system property can be used to track the callee. Better still, set the Installed JRE's JVM Args to be something like this
-Dtheflaginprod=${project_loc}
project_loc = absolute path of the the project (/usr/projects/project1)
project_path = project name relative to the workspace (/project1)

Categories

Resources