IntelliJ IDEA: How to run gradle application plugin run task with --args - java

I have a java main program in my gradle project which I can run via command line using the gradle application plugin run task with --args param new since gradle 4.9 as follows:
gradle run --args="-ahttp://foo.com/bar -dmydeviceid"
I would like to create a Run Configuration for the same in IntelliJ IDEA CE 2018.1.
I specify Task field in Run/Debug Configuration dilog as run.
I specify Arguments field as --args="-ahttp://foo.com/bar -dmydeviceid"
When I run this run config I get the error:
11:58:31 AM: Executing task 'run --args="-ahttp://foo.com/bar -dmydeviceid"'...
Unknown command-line option '--args'.
How can I make this work via run config?

The solution is to create a new Run Config as follows for the main program:
Open Run/Debug Configurations dialog vie Edit Configuration choice in pulldown left of Run button
Expand Defaults menu and select Application
Click + to create an Application run config
Enter your Main class
Enter program arguments. For my example above it was: -ahttp://foo.com/bar -dmydeviceid
Enter module in "Use classpath module". It is likely the one with suffix "_main"
Click OK
Now click run or debug button

Related

Is it possible to use an init.gradle script in IntelliJ Idea?

I'm using the Gradle wrapper in a project in Idead. I have an init.gradle file in a non-standard location and need to find a way to specify it in Idea. I thought this could be done via the Gradle VM options setting field and entered --init-script /path/to/init.gradle in it, but this throws an Unrecognized option --init-gradle /path/to/init.gralde error when trying to re-import the project.
Is there some other way to do this that I'm not aware of?
You can set Gradle commandline options/arguments from the "Run" configuration panel:
If you set this option on the "Templates>Gradle" panel it will apply to all Gradle tasks invocation ( note that you need to execute tasks with Right-click/Run from the Gradle tool window: double-click will not take the option into account..)
EDIT : another way is to add the commandline options directly in the "Run gradle task" window:

How to run single class ignoring compilation error in other class

I am using IntelliJ IDEA 2017.3. I have a project called "test", which has two classes under the src folder:
Main, has main method to print out "Hello World"
CompileErrorClass, has one method with compile errors
Those two class have no dependencies. I was trying to run the main() in the Main class, and set the before launch to "build, no error check"
I got an error message
"Error: Could not find or load main class Main".
When I fixed the compile error in CompileErrorClass, the main() worked as expected. Or if I ran the same project in Eclipse, it worked fine even with compile error.
How can I setup the run configurations in IntelliJ to run a single class ignoring the compile errors in other class without dependencies?
To be able to run code with errors you need to make a change in the settings. Go to Build, Execution, Deployment | Compiler | Java Compiler and set the Use compiler: combobox to Eclipse and enable the proceed on errors checkbox. This allows to compile classes even when they have errors.
To start a Run Configuration when the project has errors, you will need Build, no error check in the Before Launch section, instead of Build.
You can put your CompileErrorClass in a folder, then go to "Project Settings/Module" and mark the folder as "Excluded". The folder will be shown on red.
Shorcut: you can "right click" the folder an select "Mark Directory as Excluded".
In the image you can see the "bean" directory in red.
If you want to run the 'main()' method in the Main class and see the output without fixing the errors in the CompileErrorClass:
Select the 'Main' class in the project view, go-to Run --> Edit Configurations --> Before Launch and in Before Launch click + and select Build, no error check or click '-' take-out(delete) any setting in there --> Click Ok. If you take-out(using '-') any setting, then 'Before Launch' should not have any setting, its setting should be empty, null.
After the above, right-click on the Main class, and run it. You would see the output. You would see the output even if Use Compiler, which is at Settings --> Build, Execution, Deployment -->Compiler --> Java Compile --> Use Compiler, has javac selected as the compiler.
The steps are as follows:
Set Do not build before run (only need to set it once)
Menu → Run → Edit Configurations...
☑︎ Your run configuration
Run / Modify options → Java / ☑︎ Do not build before run
Recompile only selected files (remember to do this step before run)
Select packages or files that needs to be compiled.
Menu → Build → Recompile selected files (⇧ ⌘ F9)
Run or debug your run configuration.

Re-running scala program in eclipse ide after renaming

first i created a scala class inside a scala project in eclipse ide as below and ran it, it was successful.
object Test extends App{
println("hi");
}
once i rename the object , i can not find any option to run the program. [The file name is not changed and build automatically was enabled]
object Test1 extends App{
println("hi");
}
any advice ?
Try to change launch configs for your application. Clean target folder before re-build. For config you can refer to:
Select Run > Run Configurations... or Run > Debug Configurations... from the workbench menu bar. This opens a dialog
that lets you create, modify, and delete launch configurations of
different types.

Java play application running despite of having errors- how to resolve this errors?

I just started with play using activator, I created new play app using
activator new firstApp play-java
Then I opened play console using activator and run the application, app was running fine.
Then I opened the project in my intellij as SBT, the project opened and started showing errors.
in routes file there were lines
GET / controllers.HomeController.index
GET /count controllers.CountController.count
GET /message controllers.AsyncController.message
This errors are because method index, count and message are not static.
This is an obvious error since we cannot call to static method like this, why the code application is still running.
another error I am getting is in code
actorSystem.scheduler().scheduleOnce(
Duration.create(time, timeUnit),
() -> future.complete("Hi!"),
exec
);
It says this is an ambiguous method call.
My question is, if there are errors why application is running without
I created run configuration as SBT task and tried to run the app an I got bunch of errors.
These worked for me:
Open file root\build.sbt and click 'Import Project' or 'Refresh Project'.
Open file project\plugins.sbt and click 'Import Project' or 'Refresh Project'.
This will import all required libraries and after completing libraries will be visible under project view pane 'External Libraries' tree.
Go to File > Settings, select 'Play2' under 'Languages & Frameworks'.
Under right side Compiler tab check 'Use Play 2 compiler for this project' and Apply settings.

class not found exception

I'm using Eclipse; when I run my code in debug I get a class not found exception but running it without the debug it works fine.
I see it doesn't find the classes I wrote.
How can I fix this?
Double-check that the Run Configuration in Eclipse has the same classpath set for both (running it normally vs debug).
Eclipse has a lot of settings but I would try checking the following:
Run > Debug Configurations > Java Application > PROJECT_NAME > Classpath
Also, you can check your source under the same location to make sure all of your JAR files have been properly added.
I assume we're looking at a normal Java Application. Eclipse creates Run Configurations for normal execution and Debug Configurations for debug runs. And there's usually no significant difference between a Run and a Debug configuration for the same class.
The easiest way would be to start clean: Navigate to 'Run Configurations...' and delete all 'Java Application' configurations. Then do the same for 'Debug Configurations'. After this is done, start your application once in normal and once in debug mode.
Alternative: switch to a new workspace (File -> Switch workspace), import the project(s) (File -> Import... , make sure to copy content) and run/debug your application.
Check your run/launch configurations. You probably have a different configuration for debug vs run. Chances are that the debug one isn't pointing to the main class of your program.
Just check the debug... option (pull down the debug menu don't click the button, then click debug...) and the first page should have a field for "Main class". Use the same main class as in your run... option.

Categories