0

I have come across a bash script that appears to be running a java main class without compiling it or producing a binary.

Namely I am looking at the final lines of this script:

MAIN_CLASS="com.amazon.kinesis.streaming.agent.Agent"

exec $JAVACMD $JVM_ARGS "$OOME_ARGS" \
  -cp "$CLASSPATH" \
  $MAIN_CLASS "$@"

on my machine, this translates to something like this:

exec /usr/lib/jvm/jre/bin/java <some args> -cp <paths to some jars> com.amazon.kinesis.streaming.agent.Agent

I am quite unfamiliar with the exec /usr/lib/jvm/jre/bin/java <main class> format in bash. I thought java files would always need to be compiled first and then run as a .jar or class file, but this appears to be invoking the .java file directly.

How exactly does this run the file, and is there still a binary produced somewhere? I have been able to find next to nothing about this online, aside for one stack overflow answer suggesting it may be possible to run a java class from terminal.

user313
  • 681
  • 1
  • 8
  • 21
  • 1
    See [JEP 330: Launch Single-File Source-Code Programs](https://openjdk.org/jeps/330), which was delivered in Java 11. Also see [Running a Java program without compiling](https://stackoverflow.com/questions/54493058/running-a-java-program-without-compiling) and [How to launch single-file programs in Java 11 (or later)?](https://stackoverflow.com/questions/51935636/how-to-launch-single-file-programs-in-java-11-or-later?noredirect=1&lq=1). The source code is still compiled, it's just that it is compiled to, and loaded from, memory. – Slaw Aug 24 '22 at 21:12
  • 2
    However, your command does not seem to be making use of this single-file feature. The main class is set to `com.amazon.kinesis.streaming.agent.Agent`, which is a fully qualified class name, not a path to a Java source file. That class must be somewhere on the specified class-path (i.e., in one of the JAR files). Note that compiled code packaged in a JAR does not have to be launched via `-jar`. – Slaw Aug 24 '22 at 21:15
  • It makes sense to me now. Thank you! – user313 Aug 24 '22 at 21:28

0 Answers0