Search code examples
javacommand-line-argumentssystem-properties

Initializing program with files passed as main arguments (system.getProperty())


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


Solution

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