I have a few really long strings in one class for initializing user information. When I compile in Eclipse, I don't get any errors or warnings, and the resulting .jar runs fine.
Recently, I decided to create an ant build file to use. Whenever I compile the same class with ant, I get the "constant string too long" compile error. I've tried a number of ways to set the java compiler executable in ant to make sure that I'm using the exact same version as in Eclipse.
I'd rather figure out how to get the same successful compile I get in Eclipse in Ant than try to rework the code to dynamically concatenate the strings.
Solving the Problem The best way is to store our string in a separate file instead of in a declared variable or constant. Of course, we could also introduce concatenation with our string, but such isn't recommended.
So to declare a constant in Java you have to add static final modifiers to a class field. Example: public static final String BASE_PATH = "/api"; You should follow Java constant naming convention – all constant variables should be in upper case, words should be separated by the underscore.
A string constant is an arbitrary sequence of characters that are enclosed in single quotation marks (' '). For example, 'This is a string'. You can embed single quotation marks in strings by typing two adjacent single quotation marks.
Someone is trying to send you a message :-) In the time you've spend fiddling with compiler versions you could have loaded the data from a text file - which is probably where it belongs.
Check out:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With