Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java "constant string too long" compile error. Only happens using Ant, not when using Eclipse

Tags:

java

eclipse

ant

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.

like image 845
Allan Avatar asked Apr 29 '10 15:04

Allan


People also ask

How do you fix a constant string too long in Java?

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.

How do you make a string constant in Java?

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.

What is constant string?

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.


1 Answers

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:

  • java.util.Properties
  • Apache Commons FileUtils.readFileToString()
like image 130
teabot Avatar answered Sep 19 '22 17:09

teabot