dポイントプレゼントキャンペーン実施中!

---------------- sample.Main -----------------
package sample;

public class Main {
 public static void main(String[] args) {
  for (int i = 0 ; i < args.length ; i++) {
   System.out.println("args[" + i + "] = " + args[i]);
  }
  System.out.println("System.getProperty(\"sample.property\") = " + System.getProperty("sample.property"));
 }
}
----------------------------------------------

----------------- run.bat --------------------
java -cp . sample.Main %*
----------------------------------------------

---------------- ファイル構成 ----------------
currentDir
+-run.bat
+-sample
+-Main.class
----------------------------------------------

---------------- 実行と結果 ------------------
C:\>run -Dsample.property=propertyValue

C:\>java -cp . sample.Main -Dsample.property=propertyValue
args[0] = -Dsample.property=propertyValue
System.getProperty("sample.property") = null
-----------------------------------------------

------------------ 期待した結果 --------------
C:\>run -Dsample.property=propertyValue

C:\>java -cp . sample.Main -Dsample.property=propertyValue
System.getProperty("sample.property") = propertyValue
-----------------------------------------------

複雑なことは行わず(正攻法にて)
期待した結果を得るには、どのようにすればいいのでしょうか・・。

A 回答 (2件)

run.batの中身を


----------------------------------------------------------------------
java %* -cp . sample.Main
----------------------------------------------------------------------
と順番を入れ替えてみては如何でしょう。

この回答への補足

あ、そうですよね、質問の内容だと、それでも事足りてしまいますよね・・。

ごめんなさい、補足しておきますと、通常の引数はそのまま使えるように保ちつつ、システムプロパティもがっつり設定したいんです。

でも、どうしても無理な時は、Yanchさんのおっしゃるように通常引数までシステムプロパティにするしかないのかな・・。

補足日時:2008/12/04 22:23
    • good
    • 0

>ごめんなさい、補足しておきますと、通常の引数はそのまま使えるように保ちつつ、システムプロパティもがっつり設定したいんです。



でしたら、もう一工夫して、
run.batの中身を
----------------------------------------------------------------------
java -Dsample.property=propertyValue -cp . sample.Main %*
----------------------------------------------------------------------

とか、
run.batの中身を
----------------------------------------------------------------------
set OPT=-Dsample.property=propertyValue
java %OPT% -cp . sample.Main %*
----------------------------------------------------------------------
のような感じでしょうか。
    • good
    • 0
この回答へのお礼

うーん、なんだか正攻法とは言えない気がしますが、
現状(今の自分の問題)だとそれで事足りてしまうんですよね・・。

とりあえずはそんな感じでいこうかなと思います。

*必要になったら
args = obtainSystemProperties(args);
みたいなかんじでシステムプロパティを解析/設定しようと思います。

ありがとうございました。

お礼日時:2008/12/06 13:52

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!