如何在Java中正确导入类?

时间:2013-08-26 18:14:29

标签: java ubuntu

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

有问题

此处还有env输出

JAVA_HOME =的/ usr /本地/ JAVA / jdk1.7.0_10

CLASSPATH =:/家庭/ syedk / WEKA / WEKA-3-7-9 / MySQL的连接器的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) {
            }

        }
    }
}

1 个答案:

答案 0 :(得分:3)

这是问题所在:

import ScoringRequest;
import ScoringResponse;

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

作为旁注,我强烈建议反对命名一个与类相同的包,就像你在这里一样:

package Scoring;

...

public class Scoring extends HttpServlet {
    ...
}

除了其他任何内容之外,Scoring违反了包名称的Java约定。