简体   繁体   English

如何在Java中正确导入类?

[英]How do I correctly import classes in Java?

I am running into following errors while trying to compile a java program on ubbuntu 12.04. 我试图在ubbuntu 12.04上编译java程序时遇到以下错误。 . For ease of usage I have put all the classes in the same directory as the java program. 为了便于使用,我将所有类放在与java程序相同的目录中。 I will provide the error message, the main java program as well as the class listing. 我将提供错误消息,主要java程序以及类列表。 I deleted the first few comment lines hence the line numbers of the error mesage do not match. 我删除了前几条注释行,因此错误消息的行号不匹配。 I am having problem with the import statements "Scoring Request" and "ScoringResponse" 我对导入语句“评分请求”和“ScoringResponse”有问题

Also here is the env output 这里还有env输出

JAVA_HOME=/usr/local/java/jdk1.7.0_10 JAVA_HOME =在/ usr /本地/ JAVA / jdk1.7.0_10

CLASSPATH=:/home/syedk/WEKA/weka-3-7-9/mysql-connector-java-3.1.17-bin.jar 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 errors 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) {
            }

        }
    }
}

These are the problem: 这些是问题:

import ScoringRequest;
import ScoringResponse;

You don't import classes from the default package - they're just accessible already. 您不从默认包中导入类 - 它们只是已经可访问。 Just remove these two lines and it should be fine - or better, move them into a named package and import them from there. 只需删除这两行,它应该没问题 - 或者更好,将它们移动到命名包中并从那里导入它们。 Of course you don't need to import them at all if they're in the same package as the code you're importing in. 当然,如果它们与您导入的代码位于同一个包中,则根本不需要导入它们。

As a side note, I would strongly recommend against naming a package the same as a class, as you are doing here: 作为旁注,我强烈建议不要像在课程中那样命名一个与类相同的包:

package Scoring;

...

public class Scoring extends HttpServlet {
    ...
}

Aside from anything else, Scoring violated the Java conventions for package names. 除了其他任何内容, Scoring违反了包名称的Java约定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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