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

标签 java tomcat tomcat7 amazon-ami

我有一个在远程 ami 实例上的 tomcat 服务器 (v. 7) 上运行的项目。

当我尝试编译项目并在本地生成 .war 文件时,没有问题。

但是,当我尝试在远程 ami 实例上执行完全相同的操作时,出现以下错误:

[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

ServletUtilities.java的第866行内容如下:

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

对于上下文,这是围绕它的代码块:

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);

我已经在 javarepl 上本地运行此代码,没有任何问题(我使用“blah this is a test 12/28/2018”作为我的文本字符串(String text 是传递给的参数上面代码所在的方法)。

ami实例上的java版本是:

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 产生:

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"

有谁知道为什么会发生这种情况/对我接下来可以做些什么来解决问题有什么建议吗?

最佳答案

我无法重现您的问题。我的目录结构如下所示:

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

我的 pom.xml 是:

<?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>

我猜你使用的是 Tomcat 7 时 servlet 规范的 3.0。我的代码很简单:

@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 是:

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"

我找不到完全相同的 JDK - 我正在使用:

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)

您能否发布最少量的代码来重现您的问题?

编辑 - 我知道这是一种评论,但评论中不可能包含这么多信息。

关于Java 错误 : cannot find symbol: method join(java. lang.String,java.lang.String[]),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48013810/

相关文章:

java - 使用 java 创建 YouTube 事件而不提示从浏览器登录

java - 我们如何调用java匿名类中重写的方法?

java - 查看一组字节是否在 Java 中编码为 UTF-8 有什么好的启发式方法?

java - 全局 Web 应用程序参数

java - 在 Tomcat 7 中加载我的 ServletContextListener

java - 如何修复异常: getOutputStream() has already been called for this response?

java - Tomcat webapp ClassLoader 不适用于运行时编译

javax.xml.ws.WebServiceException : java. io.IOException:写入服务器 Tomcat 8 时出错

java - 从 eclipse 部署 Tomcat webapp 时出错

java - 没有定义名为 '...' 的 bean 和 Spring @Resource 注释