首页 > 解决方案 > java命令行,无法从代码中检索命令行参数

问题描述

我想检索从命令行传递的一些参数。问题是它没有按预期工作:

java -jar target/gs-rest-service-0.1.0.jar -Dhello=hello

System.out.println(System.getProperties()); // return a propertie obj with Dhello parameter inside.

所以现在,当我尝试获取值时:

  System.getProperties().getProperty("hello")); // return null
    System.getProperties().getProperty("-Dhello")); // return null 
    System.getProperty("hello"); // return null 
    System.getProperty("-Dhello")); // return null

自2小时以来,我一直在与之抗争。这开始令人恼火。

注意:这不起作用,因为 Java cl 不聪明地将参数作为属性传递。因此,它没有给我一个键/值 hello=hello,而是给了我完整的命令,没有分隔:target/gs-rest-service-0.1.0.jar -Dhello=hello(我从“sun.java.command “ 钥匙)。所以我无法检索我的参数。

标签: java

解决方案


推荐阅读