views:

137

answers:

5

Hello, 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.

+1  A: 

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
Fedearne
A: 

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
cartoonfox
I'm getting null as a result :/
d0pe
It was because of Netbeans. I'm running on an unix terminal now and it worked fine. In order to run it like this, I have to put "data.txt" in the same package as main right?Thanks in advance
d0pe
Nope I think you're going to have to put the file in the root dir of the project - I.e.directory from which the IDE runs the app.
cartoonfox
+4  A: 

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 :)

Pere Villega
A: 

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 :(

d0pe
A: 

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

d0pe