Is Java the equivalent of C # Environment.GetCommandLineArgs ()?

I know that I can get command line arguments in the "main" method, but I need to be able to indirectly .

Thank you for your help.

+8
java command-line-arguments
source share
3 answers

The following expression is exactly what you want:

System.getProperty ("sun.java.command")

+8
source share

You can list the threads, find the main thread and scan the stack trace until you find the call to the main one and pull out the arguments.

updating the comment indicates that this will not work on its own, and I think the comment is correct. I forgot about the possibilities of introspection stack or mentally mixed in the JVMTI.

So here is plan B. Connect yourself to the JMX. There are arguments in VM Summary MBean.

Connection name: pid: 77090 com.basistech.jdd.JDDLauncher -config src/main/config/benson-laptop-config.xml 

All that you have to do is call System.getProperty and live with the need to use -D to pass parameters from the outside world to your cave.

+2
source share

You can write a shell to take cli and reformat it to use -DPROP = VAL

 int main(int argc, char*argv[]) { std::vector<std::string> in (argv+1,argv+argc), out(); out.push_back("java.exe"); out.push_back("-cp"); out.push_back("my-jar.jar"); out.push_back("main.class") for( auto it = in.begin(); it!=in.end(); ++in) { //process CLI args. turn "-abc","BLAH" into "-Darg.a=true","-Darg.b=true","-Darg.c=BLAH" and push to out //Do additional processing. Maybe evn use get_opt() or Boost.ProgramOptions } //use exec or CreateProcess to launch java with the proper args //or even use something like WinRun4J methods to load the jvm.dll //Then your program shows up as "MyExe.exe" instead of "java.exe" //Use System.getProperty("arg.a","false") to get the value of a } 

Of course, you can always just tell users that you are calling a bash / batch script with the appropriate arguments -DA = true type

-one
source share

Source: https://habr.com/ru/post/651363/


All Articles