使用作为主要参数传递的文件初始化程序 (system.getProperty())

发布于 2024-08-10 07:20:54 字数 916 浏览 7 评论 0原文

我必须用这一行执行我的程序:

java -DImport=data.txt -Din=input.dat -Dout=output1.dat main.Main

我一直在试图弄清楚如何做到这一点,但无法真正让它工作。我正在使用 Netbeans,因为我不太熟悉 unix 终端类路径等。

public static void main(String[] args) {    
    String fileIn;
    fileIn = System.getProperty ("Import");
}

由于上一条语句的结果,fileIn 变为 null。

我只是从 -DImport=data.txt 开始,解决它后我会尝试其他的。


这部分已经完成了,谢谢。我将尝试将 stdin 设置为 -Din 文件而不是键盘。谢谢


我做了你说的cartoonfox,它打印 Import null 这意味着 fileIn 没有从 System.getProperty("Import"); 接收任何字符串;

我还收到此警告:

warning: [deprecation] readLine() in java.io.DataInputStream 已被弃用 行= dis.readLine();

我正在使用此页面中的代码: http://www.java-tips.org/java-se-tips/java.io/how-to-read-file-in-java.html 因为我不知道很多读者:(

I have to execute my program with this line:

java -DImport=data.txt -Din=input.dat -Dout=output1.dat main.Main

I've been trying to figure it out how to do this but can't really get it working. I'm using Netbeans since I dont really get along with unix terminal classpaths and so on.

public static void main(String[] args) {    
    String fileIn;
    fileIn = System.getProperty ("Import");
}

fileIn is getting null as a result of the previous statement.

I'm just starting with -DImport=data.txt, after I resolve it I'll try the others.


That part is done, Thank you. I'll try setting stdin as -Din file instead of keyboard. Thanks


I did what you said cartoonfox, Its printing Import null which means fileIn isnt receiving any String from System.getProperty("Import");

I am also getting this warning:

warning: [deprecation] readLine() in java.io.DataInputStream has been deprecated
line = dis.readLine();

I'm using code from this page: http://www.java-tips.org/java-se-tips/java.io/how-to-read-file-in-java.html since I dont know much of readers :(

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风月客 2024-08-17 07:20:54

我认为你混淆了两个概念:

  • Java args 指的是在类名之后传递的参数。所以如果你打电话:

java main.MyMain 0 1 2

你的args将是:

参数[0] = 0

参数[1] = 1

参数[2] = 2

  • Java 属性是您通过 -D 传递的内容,如-DImport=data.txt

在您的代码中,您将两者混合。删除线

if(args.length == 1)

它将起作用:)

I think you are confusing 2 concepts:

  • Java args refers to parameters passed after the class name. So if you call:

java main.MyMain 0 1 2

your args will be:

args[0] = 0

args[1] = 1

args[2] = 2

  • Java properties are what you are passing with -D as in -DImport=data.txt

In your code you are mixing both. Remove the line

if (args.length == 1)

and it will work :)

回忆那么伤 2024-08-17 07:20:54

删除 if (args.length == 1) ,因为您不是解析参数,而是设置系统属性。

程序参数像这样在主类之后

main.Main arg1=val arg2=val2

drop if (args.length == 1) as you are not parsing arguments, but setting system properties.

Program arguments go after the main class like this

main.Main arg1=val arg2=val2
铜锣湾横着走 2024-08-17 07:20:54

将 -Import=foo 视为将“Import”配置选项设置为“foo”值的一种方式。

只需删除 if 语句即可:

public static void main(String[] args) {

String fileIn;
fileIn = System.getProperty ("Import");
System.out.println("Import "+fileIn);
}

顺便说一句,我认为 Sun 选择了 -D (而不是 - 其他内容) )因为很多 C 编译器允许您使用 -D 在命令行上设置宏 - 这意味着它是在命令行上设置名为“常量”的一种方法...这与 Java 中的操作类似。

我不确定为什么你运行这个会得到 null,所以这是我编译并运行它的记录 - 带有输出。你将不得不看看你的内容之间的差异在这份文字记录中,我正在做的事情以及我正在做的事情:

Script started on Sat Nov  7 18:16:25 2009
bash-3.2$ cat T.java
public class T {

public static void main(String[] args) {

String fileIn;
fileIn = System.getProperty ("Import");
System.out.println("Import "+fileIn);
}
}
bash-3.2$ javac T.java
bash-3.2$ java -DImport=data.txt -Din=input.dat -Dout=output1.dat T
Import data.txt
bash-3.2$ exit
exit

Script done on Sat Nov  7 18:17:07 2009

Think of -Import=foo as a way of setting the "Import" configuration option to value "foo".

Just drop the if statement:

public static void main(String[] args) {

String fileIn;
fileIn = System.getProperty ("Import");
System.out.println("Import "+fileIn);
}

BTW I think Sun chose -D (as opposed to - something else) because lots of C compilers allow you to set a macro on the command line with -D - meaning it'd be a way of setting named "constants" on the command-line... which is similar to what it does in Java.

I'm not sure why you'd get null running this, so here's a transcript of me compiling it and running it - with output. You're going to have to look at the differences between what you're doing and what I'm doing in this transcript:

Script started on Sat Nov  7 18:16:25 2009
bash-3.2$ cat T.java
public class T {

public static void main(String[] args) {

String fileIn;
fileIn = System.getProperty ("Import");
System.out.println("Import "+fileIn);
}
}
bash-3.2$ javac T.java
bash-3.2$ java -DImport=data.txt -Din=input.dat -Dout=output1.dat T
Import data.txt
bash-3.2$ exit
exit

Script done on Sat Nov  7 18:17:07 2009
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文