3

I have a class which takes in various system parameters and prints them out:

public class Test_Class {
    public static void main(String[] args){
        String fooA = System.getProperty("fooA");
        String fooB = System.getProperty("fooB");
        String fooC = System.getProperty("fooC");

        System.out.println("System Properties:\n"+fooA+"\n"+foob+"\n"+fooC+"\n");
    }
}

Then, using IntelliJ, pass in the VM Parameters as such:

-DfooA="StringA" -DfooB="StringB" -DfooC="String C"

Upon running my program I get the following output:

System Properties:
StringA
StringB
String C

Now, if I run the same program through a UNIX server by running the following command:

java -DfooA="StringA" -DfooB="StringB" -DfooC="String C" com.foo.fooUtil.Test_Class

I get the following error:

Exception in thread "main" java.lang.NoClassDefFoundError: C

I have tried a bunch of different ways to pass in fooC, such as -DfooC=\"String C\", -DfooC='String C', -DfooC=\'String C\', basically any idea that came to mind. I have done some research and have been unable to find any solid solution.

For reference, I found the following link online where another person seems to have the same issue but, unfortunately, none of the suggestions work.

http://www.unix.com/shell-programming-scripting/157761-issue-spaces-java-command-line-options.html

How can I pass in a System Parameter with spaces in UNIX? Thank you.

Porter
  • 143
  • 2
  • 11
  • 1
    Have you tried `"String\ C"`? – kuporific Jul 01 '13 at 13:20
  • 1
    How exactly do you run the program? If you type the command as you wrote it into a shell you wont get the error you quote. – Joni Jul 01 '13 at 13:22
  • Yeah, same error message is given. – Porter Jul 01 '13 at 13:23
  • 1
    Same as @Joni. Your command line works for me. You need to give some more details on how you run the program. – Andreas Fester Jul 01 '13 at 13:25
  • Did you check [this?](http://stackoverflow.com/questions/8214392/passing-a-space-separated-system-property-via-a-shell-script-doesnt-work) PROP="-DfooC=String C" java "$PROP" -jar Foo.jar – user1573133 Jul 01 '13 at 13:26
  • Running it through PuTTy. I'm not sure if that changes anything. I'm not very good with UNIX but that is the command I run. – Porter Jul 01 '13 at 13:27
  • Still works for me - logged in to Linux server through Putty, then `java -DfooA="StringA" -DfooB="StringB" -DfooC="String C" Test_Class` prints all three properties. – Andreas Fester Jul 01 '13 at 13:31
  • 2
    [This question](http://stackoverflow.com/questions/743454/space-in-java-command-line-arguments) sounds similar to what you're experiencing, too. – kuporific Jul 01 '13 at 13:31
  • under ubunutu your code is working with OpenJDK Runtime Environment (IcedTea6 1.12.5) (6b27-1.12.5-0ubuntu0.12.04.1) and with Java(TM) SE Runtime Environment (build 1.7.0_04-b20) also is the server realy UNIX if yes which version. How you traied to use single quotation mark ? – Josef Prochazka Jul 01 '13 at 13:35
  • I hava noticed that you are running it over putty that may be the problem. Please make sure that putty character encoding is set to the same that uses the server. – Josef Prochazka Jul 01 '13 at 13:37

1 Answers1

1

Here is my approach: Why not use a .properties file for storing the system properties instead of passing them through command line? You can access the properties using:

Properties properties = new Properties();
try {
  properties.load(new FileInputStream("path/filename"));
} catch (IOException e) {
  ...
}

And you may iterate as:

for(String key : properties.stringPropertyNames()) {
  String value = properties.getProperty(key);
  System.out.println(key + " => " + value);
}

Hope that helps!!!

rahulserver
  • 10,411
  • 24
  • 90
  • 164