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

前端 未结 11 2104
抹茶落季
抹茶落季 2020-12-01 15:25

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

相关标签:
11条回答
  • 2020-12-01 15:57

    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()
    0 讨论(0)
  • 2020-12-01 15:58

    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.

    0 讨论(0)
  • 2020-12-01 16:00
      String theString2 = IOUtils.toString(new FileInputStream(new     
      File(rootDir + "/properties/filename.text")), "UTF-8");
    
    0 讨论(0)
  • 2020-12-01 16:03

    Add your string to values/strings.xml than call getResources.getString(R.string.yourstring)

    0 讨论(0)
  • 2020-12-01 16:10

    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.

    0 讨论(0)
  • 2020-12-01 16:12

    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.

    0 讨论(0)
提交回复
热议问题