简体   繁体   中英

Java Error: cannot find symbol: method join(java.lang.String,java.lang.String[])

I have project that is running on a tomcat server (v. 7) on a remote ami instance.

When I try to compile the project and generate a .war file locally, there is no issue.

But when I try to do the same exact thing on the remote ami instance, I get the following error:

[INFO] ------------------------------------------------------------- [INFO] ------------------------------------------------------------------------ [INFO] BUILD FAILURE [INFO] ------------------------------------------------------------------------ [INFO] Total time: 21.963s [INFO] Finished at: Thu Dec 28 20:01:58 UTC 2017 [INFO] Final Memory: 114M/1431M [INFO] ------------------------------------------------------------------------ [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.3:compile (default-compile) on project wildbook: Compilation failure: Compilation failure: [ERROR] /home/ubuntu/Wildbook_javaTweetBot/src/main/java/org/ecocean/servlet/ServletUtilities.java:[866,24] cannot find symbol [ERROR] symbol: method join(java.lang.String,java.lang.String[]) [ERROR] location: class java.lang.String

Line 866 of ServletUtilities.java reads as follows:

String text2 = String.join(" ", text1);

For context, here is the chunk of code surrounding it:

text = text.replaceAll("[,.!?;:]", "$0 ");
System.out.println("text: " + text);
String[] text1 = text.replaceAll("[^A-Za-z0-9 ]", "").toLowerCase().split("\\s+"); //TODO I think this does a better version of what the above (text = text.replaceAll("[,.!?;:]", "$0 ");) does??
String text2 = String.join(" ", text1);

I have run this code locally on a javarepl with no issue (I was using "blah this is a test 12/28/2018" as my text String ( String text is a parameter passed to the method in which the code above sits).

The version of java on the ami instance is:

openjdk version "1.8.0_91" OpenJDK Runtime Environment (build 1.8.0_91-8u91-b14-0ubuntu4~14.04-b14) OpenJDK 64-Bit Server VM (build 25.91-b14, mixed mode)

mvn -version yields:

Apache Maven 3.0.5 Maven home: /usr/share/maven Java version: 1.8.0_91, vendor: Oracle Corporation Java home: /usr/lib/jvm/java-8-openjdk-amd64/jre Default locale: en_US, platform encoding: UTF-8 OS name: "linux", version: "3.13.0-48-generic", arch: "amd64", family: "unix"

Does anyone know why this might be happening/have suggestions on what I can do to troubleshoot next?

I'm unable to reproduce your issue. I have a directory structure that looks like:

.
├── pom.xml
└── src
    └── main
        ├── java
        │   └── com
        │       └── example
        │           └── maven
        │               └── TestServlet.java
        └── webapp
            └── WEB-INF
                └── web.xml

My pom.xml is:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example.maven</groupId>
    <artifactId>compile-test</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.0.1</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <source>${maven.compiler.source}</source>
                    <target>${maven.compiler.target}</target>
                    <showDeprecation>true</showDeprecation>
                    <showWarnings>true</showWarnings>
                    <compilerArgument>-Xlint:all</compilerArgument>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

I guessed at 3.0 of the servlet spec as you are using Tomcat 7. My code is simply:

@WebServlet(name = "TestServlet", urlPatterns = {"/testServlet"})
public class TestServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String [] blah = {"a", "b", "c"};
        String result = String.join("-", blah);

        response.setContentType("text/plain");
        response.getWriter().println("join result is =\"" + result + "\"");
    }
}

Maven is:

Apache Maven 3.0.5 (r01de14724cdef164cd33c7c8c2fe155faf9602da; 2013-02-19 06:51:28-0700)
Maven home: /home/scott/Downloads/apache-maven-3.0.5
Java version: 1.8.0_151, vendor: Oracle Corporation
Java home: /usr/lib/jvm/java-8-openjdk-amd64/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "4.10.0-38-generic", arch: "amd64", family: "unix"

I'm unable to find a JDK that is exactly the same - I'm using:

openjdk version "1.8.0_151"
OpenJDK Runtime Environment (build 1.8.0_151-8u151-b12-0ubuntu0.16.04.2-b12)
OpenJDK 64-Bit Server VM (build 25.151-b12, mixed mode)

Can you post the smallest amount of code that reproduces your problem?

Edit - and I know this is sort of a comment but this much info isn't possible in a comment.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM