score:46
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:
score:-4
You can try this,
public static final String CONSTANT = new StringBuilder("Your really long string").toString();
score:0
Did you try this? Never tried it myself, but here is the relevant section:
Using the ant javac adapter The Eclipse compiler can be used inside an Ant script using the javac adapter. In order to use the Eclipse compiler, you simply need to define the build.compiler property in your script. Here is a small example.
<?xml version="1.0" encoding="UTF-8"?>
<project name="compile" default="main" basedir="../.">
<property name="build.compiler" value="org.eclipse.jdt.core.JDTCompilerAdapter"/>
<property name="root" value="${basedir}/src"/>
<property name="destdir" value="d:/temp/bin" />
<target name="main">
<javac srcdir="${root}" destdir="${destdir}" debug="on" nowarn="on" extdirs="d:/extdirs" source="1.4">
<classpath>
<pathelement location="${basedir}/../org.eclipse.jdt.core/bin"/>
</classpath>
</javac>
</target>
</project>
I would really consider making your classes standards compatible. I believe the official limit is 65535, and the fact that Eclipse is more lenient is something that could change on you at the most inconvenient of times, and either way constantly having to get the project compiled with Eclipse can really start to limit you in too many ways.
score:0
Add your string to values/strings.xml than call getResources.getString(R.string.yourstring)
score:0
I was able to resolve this issue in a similar way like Lukas Eder.
String testXML = "REALLY LONG STRING...............................";
textXML += "SECOND PART OF REALLY LONG STRING..........";
textXML += "THIRD PART OF REALLY LONG STRING............";
Just split it up and add it together;
score:0
permanent solution would be for this is getting long string from file specially when you are writing test case in spring projects. the below one worked for me
File resource = new ClassPathResource(
"data/employees.dat").getFile();
String employees = new String(
Files.readAllBytes(resource.toPath()));
score:1
String theString2 = IOUtils.toString(new FileInputStream(new
File(rootDir + "/properties/filename.text")), "UTF-8");
score:1
Another trick, if I'm determined to put a long string in the source, is to avoid the compiler detecting it as a constant expression.
String dummyVar = "";
String longString = dummyVar +
"This string is long\n" +
"really long...\n" +
"really, really LONG!!!";
This worked for a while, but if you keep going too far the next problem is a stack overflow in the compiler. This describes the same problem and, if you're still determined, how to increase your stack - the problem seems to be the sheer size of the method now. Again this wasn't a problem in Eclipse.
score:4
A workaround is to chunk your string using new String()
(yikes) or StringBuilder
, e.g.
String CONSTANT = new String("first chunk")
+ new String("second chunk") + ...
+ new String("...");
Or
String CONSTANT = new StringBuilder("first chunk")
.append("second chunk")
.append("...")
.toString();
These can be viable options if you're producing this string e.g. from a code generator. The workaround documented in this answer where string literals are concatenated no longer works with Java 11
score:6
The length of a string constant in a class file is limited to 2^16 bytes in UTF-8 encoding, this should not be dependent on the compiler used. Perhaps you are using a different character set in your ant file than in eclipse, so that some characters need more bytes than before. Please check the encoding
attribute of your javac
task.
score:15
Nothing of above worked for me. I have created one text file with name test.txt and read this text file using below code
String content = new String(Files.readAllBytes(Paths.get("test.txt")));
score:45
I found I could use the apache commons lang StringUtils.join( Object[] ) method to solve this.
public static final String CONSTANT = org.apache.commons.lang.StringUtils.join( new String[] {
"This string is long",
"really long...",
"really, really LONG!!!"
} );
Source: stackoverflow.com
Related Query
- Java "constant string too long" compile error. Only happens using Ant, not when using Eclipse
- Ant throws error when String is too long
- Using Eclipse IDE can compile and run the Java program perfectly but when I use 'javac' it is resulting to Error
- Import Error in Java Using Ant but not Eclipse
- How to debug Java code when using ANT script in Eclipse
- Java SE 11 String Final Variable with Ternary Operator Does Not Count as a Constant Variable in Switch Case Expression
- How can I fix the "Property 'allowUndeclaredRTE' does not exist" error when using my Eclipse Checkstyle plugin?
- Developing with GWT (in eclipse) when NOT using a Java Backend
- Error When Deploying Java Appp to App Engine? Can not get the System Java Compiler. Please use a JDK, not JRE?
- Java won't open the correct file, constantly returns the File Not Found Exception even when using the absolute path
- Where does Ant look to find your Java Home when using Eclipse?
- Why is my java code only using about 20% of CPU when there is no GUI
- Why am I getting a SLF4J Error when I'm not using it?
- Eclipse Tycho: Compile error when using the java.xml.bind module?
- Trying to remotely compile, using the command line, a Java program with multiple dependencies that I can currently only compile locally in Eclipse
- Syntax error in Eclipse when using static getNames method of JSONObject Java class
- Eclipse Juno error when using console view in java browsing perspective
- how to validate integers or Long values,so that it should accept only numbers not characters using annotations in Hibernate?
- Error when using ical4j.jar Java
- Why I have these errors when I try to compile using this Ant script?
- Path too long error when trying to run Java/Eclipse app on azure emulator
- getting error when using controller in fxml file JAVA Code
- java externals jars not included when using maven?
- Java Class files in tomcat not linking/updating when using eclipse
- Ant task: In ant build i am using java task, where i used system.out.println which are not appearing in the eclipse console
- Python script runs fine from command line, file not found error when using Eclipse and PyDev
- Eclipse error 'could not determine Java version' when importing project
- "Error: Could not find or load main class Average" When Using Referenced Assembly in Java Code
- Printing Unicode characters using Java in eclipse works, but not when I export to a jar
- Eclipse error when trying to run Java application - "Selection does not contain a main type" - But it does?
More Query from same tag
- Exception in thread "Thread-2" java.lang.NullPointerException
- Classpath of a decompiled Jar file
- make eclipse work better with javascript
- How to open file in META-INF independent of environment
- Stop eclipse from resetting root context when Maven->Update Project / MANIFEST.MF file not found
- Google Play Services - Sign in - Client ID debug vs release
- How to delete a node from a linked list
- How to show margin of block in Eclipse?
- How to set priority on button in android
- 105 errors java android eclipse
- Eclipse: How to CLOSE a console View?
- Eclipse WebApp deploying over Maven with Tomcat results in Error: 'Failed to deploy application at context path'
- "NLS missing message" Error for Eclipse while installing it
- ADT shows an error in Strings.xml,how to rectify that particular error?
- Mac C++/Mars eclipse gdb debug launching stuck at 96%
- Can DataNucleus Enhancer process classes that are in jar on a classpath?
- Integrating ZbarScanner with android app
- Any WYSIWYG editor based on XULRunner and SWT.Mozilla concept, JavaXPCOM
- NullPointerException error
- How to remove default checkstyle in eclipse?
- When did java files Compile in Eclipse ide ? and when is the .class file created in Eclipse IDE
- change the space between if() and { in Eclipse
- Can not open Workspace in Enterprise Edition Studio on Windows, but I can on Community Edition Studio
- Android: Eclipse: The following classes could not be instantiated: - com.google.android.gms.common.SignInButton
- Hibernate 5 + Eclipse RCP + SQLite 3 - Unable to resolve name as strategy org.hibernate.dialect.Dialect
- what is the correct file path in eclipse spring
- How to Create AVD on version 4.0 APi 14 in Eclipse
- Google maps Places API not working
- Extract interfaces from a class
- Handling android application in Eclipse