1

java "$homeOption" -cp "$classPath" "com.civilizer.extra.tools.DataBroker" -import "$importPath"

If $homeOption is not empty, the command above works, but $homeOption is empty, it can't find the main class

Error: Could not find or load main class

Looks like Empty $homeOption parameter affects classpath string in a bad way; It's so strange behavior to me;

Anyone running into this issue and understanding why?

Edit:

In case that it works:

The actual command line is as follows;

com.civilizer.extra.tools.DataBroker is a Java class with main method, and it is included in that verbose classpath;

in this case, $homeOption is -Dcivilizer.private_home_path=/Users/bsw/.civilizer

java -Dcivilizer.private_home_path=/Users/bsw/.civilizer -cp /Users/bsw/test/trysomething/civilizer/target/civilizer-1.0.0.CI-SNAPSHOT/WEB-INF/classes:/Users/bsw/test/trysomething/civilizer/extra/lib/:/Users/bsw/test/trysomething/civilizer/target/civilizer-1.0.0.CI-SNAPSHOT/WEB-INF/lib/:/Users/bsw/test/trysomething/civilizer/target/extra com.civilizer.extra.tools.DataBroker -import

In case that it can't find the main class:

java -cp /Users/bsw/test/trysomething/civilizer/target/civilizer-1.0.0.CI-SNAPSHOT/WEB-INF/classes:/Users/bsw/test/trysomething/civilizer/extra/lib/:/Users/bsw/test/trysomething/civilizer/target/civilizer-1.0.0.CI-SNAPSHOT/WEB-INF/lib/:/Users/bsw/test/trysomething/civilizer/target/extra com.civilizer.extra.tools.DataBroker -import

As I mentioned, only $homeOption is empty; but it just makes the issue; BTW, even if $homeOption is empty, the class will run without a problem, but you know, the main method is missing in the first place in this case, it doesn't matter

3
  • BTW, it was tested on OS X Bash Commented Jan 10, 2017 at 10:26
  • IS DataBroker the class with the main method? If not, what is the name of the class that has the main method. What is the value set in homeOption when it is not empty. Please share all relevant information that will help you to get to the bottom of this. Commented Jan 10, 2017 at 10:34
  • @CKing sorry... I edited the question Commented Jan 10, 2017 at 11:15

1 Answer 1

2

You could resolve this by populating an array and passing that to the java command instead.

opts=( )
if [[ -n "$homeOption" ]]; then
    opts+=( "$homeOption" )
fi

java "${opts[@]}" -cp "$classPath" "com.civilizer.extra.tools.DataBroker" -import "$importPath"

The issue you are seeing is because bash is passing a blank string to java as the first argument, and java is taking the blank string to be the class:

compare:

$ java foo
Error: Could not find or load main class foo

vs:

$ java ''
Error: Could not find or load main class

You can see that java prints the class name it can't find in the error, but your case, and my second case above, the class name is an empty string, so the class name is blank in the error message as well.

The reason my solution works is if the array is empty then bash won't pass in any empty arguments. And the array is created empty, and left empty unless $homeOption has a non-empty string.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, looks perfect!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.