Question

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.

Était-ce utile?

La solution

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?

Autres conseils

I suggest you take a look at the documentation of System.getProperty().

Basically it retrieves a value from the system, either already present or set by you.

To avoid retrieving null you can use another method signature that specify a default value:

System.getProperty("import", "file.txt");

To set a System property, you can specify it at launch:

java -Dimport="file.txt" your_application

or set it programatically :

System.setProperty("import", "file.txt");

Maybe system properties are not the most indicated way to do that (depends on your application).
You could also use command line arguments to pass the file name to your main method:

public class CommandLineExample {

    public static void main(String[] args) {
        if (args.length < 1) {
            System.err.println("usage: CommandLineExample <filename>");
            System.exit(1);
        }

        String filename = args[0];
        if (filename !=null && !filename.isEmpty()) {
            ...
        }
    }

}

Your program should be called as:

    java CommandLineExample theFileName

the string "theFileName" will be passed to the main method in args[0] (any additional words will be passed in subsequent positions of args {args[1], args[2], ...})

EDIT
if the program must be called with

    java -Dimport=filename ... 

then System.getProperty("import") will return the filename.
Confirm that you are calling the correct program (class name, package, version, last compile was successful, ...) and also check that the property is not mistyped like java -Dinport=A-... or has additional spaces, uppercase letters...

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top