-1

The only way to get to the command line arguments in a plain Java program is the main entry method. Neither java.lang.System nor java.lang.Runtime have methods to access the args.

  • There is a -Dkey=value subset of command-line args, which JDK parses and interprets as "system properties", and there is indeed API to get these. For the regular program args there is however no such API whatsoever.

  • There is a spurious sun.java.command system property, which works in plain Java but does not seem to work in a running Equinox (tried with Oracle JDK 1.8.0_31). S. this topic Is it possible to get the command used to launch the jvm in java?

  • In Equinox they introduced the EnvironmentInfo service, which provides such API, but its implementation is weird (mix of an OSGi service and public-static) and buggy in the way that the service is very dependent on the way OSGi Framework Launcher initializes it. We have used it till recent, but now ran into a very unpleasant issue where the service is there but is not yet statically initialized by the (BND) launcher.

Why there is no any standard mean to access the command line args in Java in a static way from anywhere in the code? Is it a JDK design flaw or was there some intentional thought behind it?

Community
  • 1
  • 1
Ilya Shinkarenko
  • 2,134
  • 18
  • 35
  • On which other way would you need to access the arguments? it seems to me that, true the main method, IS a standard way to get the command line args. – Stultuske May 20 '15 at 08:59
  • It surely is, but in a container environment I do not have any access to the main, so I would love to have somehing like System.getCommandLineArgs() (analog to getting environment and the system properties) – Ilya Shinkarenko May 20 '15 at 09:01
  • What is stopping you from calling a setter to a static variable in some class available for the entire application ? – Stultuske May 20 '15 at 09:03
  • One could say that a container is design to be *headless* process and should rely only on config/properties file while a standalone/desktop app should use the command line parameters – ortis May 20 '15 at 09:05
  • Java makes the plain arguments available to the main. If the container library decided not to give you an API, it's the container library's design decision. – RealSkeptic May 20 '15 at 09:06
  • Yes. What is the reason for such design decision? – Ilya Shinkarenko May 20 '15 at 09:08
  • What's the point in asking why? It is what it is. There's lots of crap that I hate in Java, but I just have to deal with it. – Neil Bartlett May 20 '15 at 12:56
  • There's always a point in asking WHY. Probably, SO is not the proper platform for questions like this, people tend more to ask HOW here. "Crappy decision" is a valid answer though, I do accept it and stop looking for the hidden meanings. – Ilya Shinkarenko May 20 '15 at 13:02

2 Answers2

1

In a container you would use system properties instead of arguments. You can set the system properties using -Dkey=value. Apart from that there simply is not standard java API to get the arguments except for the main method.

Christian Schneider
  • 19,420
  • 2
  • 39
  • 64
  • Yes, this is the fact and I am aware of it. The question was why is it designed in this way? Why would one want to separate regular command line arguments and their -D*** subset, which Java interprets as system properties. – Ilya Shinkarenko May 20 '15 at 09:37
  • 1
    I guess this is something only the java designers at sun / oracle can answer. – Christian Schneider May 20 '15 at 11:23
-3

if you mean the arguments to your application, you get them in "args":

public static void main(String[] args) {
           //...
}

if you are talking about arguments to the VM - well, those aren't really your business, right? they might depend on the supplier of the VM, they may be platform-dependent ... lots of reasons to want isolation there.

rmalchow
  • 2,689
  • 18
  • 31
  • Pls read the very first sentence of the question. I do know about the main method, but in a pluggable container environment (like OSGi or any application server) I do not have access to this method. – Ilya Shinkarenko May 20 '15 at 09:06
  • oops, sorry ... well - but OSGi is also about isolation - basically, each OSGI module has a defined contact surface to the rest of the world. removing as many dependecies on the runtime around it as possible seems like a reasonable thing to do? – rmalchow May 20 '15 at 09:08
  • jap, but since we naturally have access to the env variables, why not to give access to command line args? there might be some deep philosophy behind which I do not get – Ilya Shinkarenko May 20 '15 at 09:10