javaurlencode

Program works in Eclipse, but not as a jar


My compiler is set to be compliant with java 1.8. When I run my code, it works and there are no warnings.

When I go to export everything as a runnable jar and execute it, it throws this error:

Exception in thread "main" java.lang.NoSuchMethodError: java.net.URLEncoder.encode(Ljava/lang/String;Ljava/nio/charset/Charset;)Ljava/lang/String;
        at ...

My code needs to convert some user-input String to make an API-call, for which it needs to be URL compliant, e.g. by replacing spaces with '%20'. I am using java.net.URLEncoder.encode for this.

My system has jre version 1.8.0_411 installed so this really shouldn't be happening, right?


Solution

  • There are 3 barely related aspects which all combine into '.. would run on java 1.8':

    You can't just tell eclipse 'hey, uh, please, only 1.8 stuff' and expect that final bullet to 'just work' - knowing what is and is not available on java 1.8 is complicated, because the standard library does not actually ship with the 'this has been here since version X' information, so, eclipse can't go: Oh, hey now hold on a second you are using something that wasn't in java 1.8. It seems like it is there (javadoc @since tags) but these aren't guaranteed by the java spec.

    Hence, eclipse needs an actual java 1.8 installation in order to know. You say you have one - okay, great! Tell eclipse about it, and then it can help you.

    The procedure is as follows: