Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java System Parameter causing NoClassDefFoundError

Tags:

java

unix

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.

like image 980
Porter Avatar asked Apr 17 '26 13:04

Porter


1 Answers

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!!!

like image 76
rahulserver Avatar answered Apr 20 '26 04:04

rahulserver



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!