1

When i am running java program from Intellij idea i noticed that they are executed via reflection by a class called AppMain (from idea_rt.jar).

IntelliJ product spec:

IntelliJ IDEA (Community Edition) IC-145.184.1 Build #IC-145.184, built on March 1, 2016 JRE: 1.8.0_60-b27 x86

For E.g: if i execute below code ,

public class Example{


    public static void main(String[] args) {
        throw new RuntimeException();
}
}

Following is the output in which there are 5 internal calls from AppMain

Exception in thread "main" java.lang.RuntimeException at Example.main(Example.java:12) at > sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:497) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

As you can see above all the stack trace info has 5 extra stack trace information origination from AppMain class from idea_rt.jar

Is there anyway to Run Java Applcation's from IntelliJ IDE without using AppMain ?

chebus
  • 762
  • 1
  • 8
  • 26
  • Why do you care? You can always run it as a standalone with just the JRE. But since you're using IDEA, why on earth would you even be surprised that it's the one that's starting your application? Are you trying to minimize the stack usage or what (and if so, for what reason)? – Kayaman Mar 21 '16 at 12:45
  • I am storing the stack trace in my application , every time i take the stack trace it would give me this extra 5 lines which is annoying..so either i need to manually skip them ( which binds the code to only one IDE and later delete the this code ) . So i need a way not to run the Java Application in IDEA without using AppMain So on a side note: When i just execute the application with Debug configuration then the extra info is not displayed. – chebus Mar 21 '16 at 12:55
  • 1
    Why not just examine the stacktrace and only save the frames starting from `Example.main()`? Your approach to this "problem" is quite poor if you attempt to solve it through the IDE. – Kayaman Mar 21 '16 at 13:09
  • I can directly store result i.e. stack frames from getStackTrace of current thread without filtering the extra frames internal to the IDEA weren't there. But i am curious if there is anyway to run application without using AppMain. When i run this example code in eclipse it would only give frames without any ide specific frames – chebus Mar 21 '16 at 13:20

1 Answers1

3

There are a couple of ways to achieve this. If you're using gradle you can follow the instructions in the answers here: gradle run, or if you're using maven you can follow the answers here: maven run. Once you have configured the maven or gradle solution you can run it from the tool window associated with the build tool you are using, or you can use the same window to assign a keyboard shortcut to the run task.

Those solutions are helpful because they construct the classpath for you. If you're willing to construct it manually in some way (perhaps by copying it from the run window when you ask IntelliJ to run the method for you), then you could write your own shell script and ask IntelliJ to run that, either by adding it to your path in some way and typing its name in the shell tool window, or by adding a new 'Application' run configuration that references the script.

However, the earlier comments are still correct -- you shouldn't be doing this. I'm assuming that in whatever you consider production you'll not be running the app from intellij? So, this isn't a production problem and it's only really bothering you personally, in which case you should ignore it and make better use of your time.

Community
  • 1
  • 1
Software Engineer
  • 15,457
  • 7
  • 74
  • 102
  • 2
    I agree with your viewpoints. It is just that i was "tightly coupled" with IDEA IDE that the thought staying away from it scared me :) I do agree i shouldn't have wasted any time , so as a work around i am just executing the programs by debugging(turn off breakpoints) which is surprisingly not using AppMain and there by related frames are not shown in the stack trace. or by another alternative i have constructed my own VM arguments excluding AppMain which is executing program directly by java tool. – chebus Mar 21 '16 at 18:58
  • Also lost a few hours on avoiding AppMain as Main Class. It's avoided in Debug mode. Thanks for sharing it with us. – Jurica Krizanic Oct 25 '16 at 12:19