【问题标题】:Why is the System.getProperty not working in eclipse + Java 8?为什么 System.getProperty 在 Eclipse + Java 8 中不起作用?
【发布时间】:2014-08-19 05:40:50
【问题描述】:

所以在 Eclipse 的运行配置中,我选择了我的主类 Main.java,在“程序参数”选项卡中我输入了 testArg=Sample

在我的应用程序的main 方法中,我执行以下操作: System.out.println 开启:

  1. args[0]
  2. System.getProperty("testArg")

args[0] 得到的结果是testArg=Sample,但System.getProperty("testArg") 得到的结果是null

发生了什么事?我正在使用 Java 8 和 Eclipse Kepler。

注意:

当我输入“VM Arguments”框 testArg=Sample 时,我得到错误:

错误:无法找到或加载主类 testArg=Sample`

【问题讨论】:

  • System.getProperty 是做什么的?
  • 导入 java.util.Properties;
  • 我不认为提供testArg=Sample 作为参数可以达到你认为的效果。
  • 如果 testArg 的值是多少?
  • 使用java -Dtestarg=Sample ...设置系统属性

标签: java eclipse java-8


【解决方案1】:

System.getProperty() 获取通常用 -D 指定的 VM 参数,这些参数通常用于配置 VM。您的论点是“程序论点”。

见:What's the difference between program arguments and VM arguments?

当您使用 eclipse 时(取决于版本);您应该能够在参数选项卡下的同一位置为运行配置指定 VM 参数和程序参数:

  • 程序参数 - 作为 args[] 传递给您的主要方法
  • VM 参数 - 可通过 System.getProperty() 获得

【讨论】:

  • thkx -D 是我没有得到的
【解决方案2】:

java -h

Usage: java [-options] class [args...]
          (to execute a class)
  or  java [-options] -jar jarfile [args...]
          (to execute a jar file)
...
   -D<name>=<value>
             set a system property

所以,你想要

java -DtestArg=Sample -cp MY_CLASSPATH mypackage.MyMain

或者,在日食中-

转到“首选项 -> Java -> 已安装的 JRE”。您可以在那里设置 JRE 安装并更新 JVM 参数。

【讨论】:

    猜你喜欢
    • 2013-10-07
    • 2017-01-22
    • 1970-01-01
    • 1970-01-01
    • 2012-11-05
    • 1970-01-01
    • 2011-11-01
    • 1970-01-01
    相关资源
    最近更新 更多