Rita Pereira Rita Pereira - 10 days ago 7
Java Question

Using the java System.getProperty("Import")

I was doing some work for college and my main runs this:

Spreadsheet sheet = new Spreadsheet(0,0);
SpreadsheetManager manager = new SpreadsheetManager(sheet);

/* Read an Import file, if any */
String filename = System.getProperty("import");

if (filename != null)
sheet.parseInputFile(filename, sheet);


Thing is, when I actually try to import a file it doesn't do what is supposed to and the filename is always null, so it never reaches my parseInputFile.

My teachers made a bunch of code for different programming exercises that do similar things available, and I've also looked at projects my colleagues did in previous years, but every single one does what I am doing above.

I have to run my program like this:
java -Dimport=A-002-002-M-ok.import calc.textui.Calc
otherwise none of the tests given by the teachers will run.

I'm sorry if this is not a useful question, but I've tried looking everywhere. If anyone could explain how the
System.getProperty("import")
works and why it isn't working in this case, I would be very grateful.

Answer

When you run your program with:

java -Dimport=foo

then the method call

System.getProperty("import")

should return "foo".

Is ist possible that you write a tiny example program to convince yourself? Without any SheetManagers and all stuff, just

class ItWorks {
public static void main(String[] args) {
   System.out.println(System.getProperty("import"));
}
}

Call it thus

java -Dimport=indeed ItWorks

and report what happens.

That being said: if you want to pass command line arguments, why don't you use the facility for command line arguments? (i.e. the String[] array passed to main?)

You could then call your program like this:

java calc.textui.Calc my-nice-spreadsheet.data

=====================================================

Please write the follwoing in your calc.textui.Calc program immediately after the open brace of your class definition:

public class Calc ..... {   // a line like this already exists
    // insert next line here
    public static String filename = System.getProperty("import");

    // rest of your class, as before.
}

Then comment out the getProperty() line in your method that didn't work, but leave the rest including the System.out.println(filename);

Does it change?

Comments