Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect if my application was launched from IntelliJ?

I can launch my app as standalone app (for example from command line) or one specific way -- it is directly "from" source code, i.e. using IntelliJ.

Is there a way to detect if app was launched from IntelliJ or not? Only this, binary decision.

Workarounds are plenty, adding extra option when launching, however I am looking for direct solution, like this for C# How to know that the application is launched by debugger (VisualStudio) C#

The solution can be valid for any IDE, but must be valid at least for IntelliJ.

Little update

For those (including me) seeking ready to use code (Scala code):

object ProgramInfo
{
  val isStandaloneApp = sun.management.ManagementFactory.getRuntimeMXBean().getInputArguments.isEmpty
  val isDebugMode = scala.collection.JavaConversions.iterableAsScalaIterable(sun.management.ManagementFactory.getRuntimeMXBean().getInputArguments).exists(it => it.startsWith("-agentlib"))
}

It won't work in complex cases, but if you don't pass ton of parameters to JVM, it's just fine.

like image 593
greenoldman Avatar asked Dec 07 '11 15:12

greenoldman


People also ask

How do I debug an application in IntelliJ?

Run the program in debug modeClick the Run icon in the gutter, then select Modify Run Configuration. Enter arguments in the Program arguments field. Click the Run button near the main method. From the menu, select Debug.


2 Answers

I have used the below in the past to check the debug settings. You could use something similar

ManagementFactory.getRuntimeMXBean().getInputArguments()

enter image description here

like image 109
Amir Raminfar Avatar answered Sep 21 '22 10:09

Amir Raminfar


When I run an application in IDEA, it sets some variable like -Didea.launcher.port, and -Didea.launcher.bin.path. Maybe you could check if they are set or not.

like image 23
Christian Avatar answered Sep 18 '22 10:09

Christian