6

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.

Community
  • 1
  • 1
greenoldman
  • 16,895
  • 26
  • 119
  • 185
  • I assume this can be adapted to IntelliJ: http://stackoverflow.com/questions/1109019/determine-if-a-java-application-is-in-debug-mode-in-eclipse – mikerobi Dec 07 '11 at 15:13
  • 1
    Why don't you want to simply add a JVM option like `-DideLaunch`? This seems to be the cleanest solution to me, since it does not rely on fishy IDE- or JVM- or OS-dependent peculiarities. – ziggystar Dec 07 '11 at 15:21
  • @mikerobi, thank you, as I said in post, this is workaround and it has ugly property that you have to configure every app. Since I use options for my app, I could pass another one as well. – greenoldman Dec 07 '11 at 15:27
  • @ziggystar, I just answered about adding extra options. Why ruling out IDE? I see no point, I use only IntelliJ. – greenoldman Dec 07 '11 at 15:27
  • Other agentlibs exists. – Thorbjørn Ravn Andersen Oct 21 '15 at 15:07

5 Answers5

10

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

Amir Raminfar
  • 33,777
  • 7
  • 93
  • 123
2

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.

Christian
  • 4,543
  • 1
  • 22
  • 31
  • Thank you. Do you read such variable via "System.getProperties"? If yes, there is no such variable as you mentioned set. – greenoldman Dec 07 '11 at 15:45
  • 1
    This works for me: System.getProperty("idea.launcher.bin.path") or System.getProperty("idea.launcher.port"). Remark: I run scala code, but I think there should be no difference. – Christian Dec 07 '11 at 15:49
  • I write in Scala too (that's why the tag), but in my case I get nulls. IntelliJ 10.5.3, openSUSE 11.4, JDK 1.6. – greenoldman Dec 07 '11 at 16:06
1

You can't do this in a generic way, because Intellij launches a java app in exactly the same way as you would from your command line. Well not exactly the same, but the differences can't be counted upon.

  1. You can detect from your app if a debugger is attached (see Amirs answer), but this isn't necessarily Intellij, could be something else like jvisualvm. This is the same restriction as in Visual Studio and C#, if I understand correctly. And if you don't launch in debug mode, this won't work.

  2. You can always launch in Intellij with a -Dlaunchedfromintellij, as suggested by Christian, but this isn't consistent, and as you said it's a pain to do for all apps.

  3. Third suggestion is, when you launch Intellij, or Eclipse, ensure that a particular environmental variable is set, for linux:

    $ LAUNCHED_FROM_IDE=something intellij

Then any launched processes from intellij will inherit this environmental variable, but ones from the command line won't. It doesnt't matter what the value is, because you're testing that it exists.

Matthew Farwell
  • 60,889
  • 18
  • 128
  • 171
  • Matthew, you mean I should set it manually for each app I am working on? This is just a workaround, and I already mentioned it -- it does not scale up, because with every app you have to remember to add it again. I am looking something which is tied up to IntelliJ, which allows to tell the difference. – greenoldman Dec 07 '11 at 15:50
  • 1
    2 yes, you're right. 3, you're setting the environmental variable when you launch Intellij, so only needs to be done once. – Matthew Farwell Dec 07 '11 at 16:08
  • Oh, I missed that. Thank you for help! I will keep Amir answer as solution because it works for me, and it is one step less. And I am already using it :-) I hope it is OK with you. – greenoldman Dec 07 '11 at 16:29
0

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

Here is the solution I'm using.

Community
  • 1
  • 1
BullyWiiPlaza
  • 17,329
  • 10
  • 113
  • 185
0

I am using this in my JUnit test.

Assume.assumeTrue("this test is executed in IntelliJ", Boolean.getBoolean("intellij.debug.agent"));

Basically it detects the system property when running from IntelliJ in debug mode.

Patrick
  • 799
  • 1
  • 10
  • 14