1

尝试在 ubbuntu 12.04 上编译 java 程序时遇到以下错误。. 为了便于使用,我将所有类与 java 程序放在同一目录中。我将提供错误消息、主要的 java 程序以及类列表。我删除了前几行注释,因此错误消息的行号不匹配。我对导入语句“评分请求”和“评分响应”有问题

这里也是环境输出

JAVA_HOME=/usr/local/java/jdk1.7.0_10

CLASSPATH=:/home/syedk/WEKA/weka-3-7-9/mysql-connector-java-3.1.17-bin.jar

Error message 
=============
javac Scoring.java


Scoring.java:37: error: '.' expected
import ScoringRequest;
                     ^
Scoring.java:37: error: ';' expected
import ScoringRequest;
                      ^
Scoring.java:38: error: class, interface, or enum expected
import ScoringResponse;
       ^

3 个错误

package Scoring;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StringWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ScoringRequest;
import ScoringResponse;

@WebServlet( name="Scoring", displayName="Scoring Servlet", urlPatterns = {"/Scoring"}, loadOnStartup=1)
public class Scoring extends HttpServlet {

    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        BufferedReader reader = null;
        BufferedWriter writer = null;
        ScoringRequest req = null;

        try {
            reader = new BufferedReader(new InputStreamReader(
                    request.getInputStream()));

            StringBuffer xml = new StringBuffer();
            String line = reader.readLine();
            while (line != null) {
                xml.append(line);
                line = reader.readLine();
            }

            req = new ScoringRequest(xml.toString(), null, null,
                    null);

            ScoringResponse res = (new ScoringEngine()).score(req);

            writer = new BufferedWriter(new OutputStreamWriter(
                    response.getOutputStream()));
            writer.write(res.toXML());
            writer.flush();

        } catch (Exception ex) {
            System.err.println(ex);

            ScoringResponse res = new ScoringResponse(req.getModelName(), req.getPmmlURL(), req.getCsvInputRows(), null);
            StringWriter errWriter = new StringWriter();
            ex.printStackTrace(new PrintWriter(errWriter));
            res.setErrorMessage(errWriter.toString());
//          res.setErrorMessage(ex.getMessage());

            writer = new BufferedWriter(new OutputStreamWriter(
                    response.getOutputStream()));
            writer.write(res.toXML());
            writer.flush();

        } finally {
            try {
                if (reader != null)
                    reader.close();
            } catch (Exception ex) {
            }

            try {
                if (writer != null)
                    writer.close();
            } catch (Exception ex) {
            }

        }
    }
}
4

1 回答 1

3

这些是问题:

import ScoringRequest;
import ScoringResponse;

您不会从默认包中导入类 - 它们只是可以访问的。只需删除这两行就可以了 - 或者更好的是,将它们移动到命名包中并从那里导入它们。当然,如果它们与您要导入的代码在同一个包中,则根本不需要导入它们。

作为旁注,我强烈建议不要将包命名为与类相同的名称,就像您在此处所做的那样:

package Scoring;

...

public class Scoring extends HttpServlet {
    ...
}

除此之外,Scoring它还违反了 Java 包名称约定。

于 2013-08-26T18:18:40.680 回答