2016-08-13 3 views
1

Mein Projektverzeichnis wie folgt aussieht:Javac "kann kein Symbol finden" Warum?

predictions 
--> WEB-INF 
    --> classes 
     --> predictions 
      --> Predictions.java, 
       Prediction.java, 
       Prediction.class 

Prediction.java

package predictions; 

import java.io.Serializable; 

public class Prediction implements Serializable { 

    private static final long serialVersionUID = 1L; 
    private String who; 
    private String what; 

    public Prediction(){} 
    public String getWho() { return who; } 
    public void setWho(String who) { this.who = who; } 
    public String getWhat() { return what; } 
    public void setWhat(String what) { this.what = what; } 

} 

Predictions.java

package predictions; 

import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.io.BufferedReader; 
import java.io.ByteArrayOutputStream; 
import java.beans.XMLEncoder; // simple and effective 
import javax.servlet.ServletContext; 

public class Predictions { 
    private int n = 32; 
    private Prediction[ ] predictions; 
    private ServletContext sctx; 

    public Predictions() { } 

    // The ServletContext is required to read the data from 
    // a text file packaged inside the WAR file 
    public void setServletContext(ServletContext sctx) { 
    this.sctx = sctx; 
    } 
    public ServletContext getServletContext() { return this.sctx; } 

    // getPredictions returns an XML representation of 
    // the Predictions array 
    public void setPredictions(String ps) { } // no-op 
    public String getPredictions() { 
    // Has the ServletContext been set? 
    if (getServletContext() == null) 
     return null;  

    // Have the data been read already? 
    if (predictions == null) 
     populate(); 

    // Convert the Predictions array into an XML document 
    return toXML(); 
    } 

    //** utilities 
    private void populate() { 
    String filename = "/WEB-INF/data/predictions.db"; 
    InputStream in = sctx.getResourceAsStream(filename); 

    // Read the data into the array of Predictions. 
    if (in != null) { 
     try { 
     InputStreamReader isr = new InputStreamReader(in); 
     BufferedReader reader = new BufferedReader(isr); 

     predictions = new Prediction[n]; 
     int i = 0; 
     String record = null; 
     while ((record = reader.readLine()) != null) { 
      String[] parts = record.split("!"); 
      Prediction p = new Prediction(); 
      p.setWho(parts[0]); 
      p.setWhat(parts[1]); 

      predictions[i++] = p; 
     } 
     } 
     catch (IOException e) { } 
    } 
    } 

    private String toXML() { 
    String xml = null; 
    try { 
     ByteArrayOutputStream out = new ByteArrayOutputStream(); 
     XMLEncoder encoder = new XMLEncoder(out); 
     encoder.writeObject(predictions); // serialize to XML 
     encoder.close(); 
     xml = out.toString(); // stringify 
    } 
    catch(Exception e) { } 
    return xml; 
    } 
} 

ich die Prediction.java zusammengestellt -> Predictions.class. Aber Predictions.java wirft Fehler:

symbol: class Prediction 
    location: class Predictions 
Predictions.java:52: error: cannot find symbol 
       predictions = new Prediction[n]; 
           ^
    symbol: class Prediction 
    location: class Predictions 
Predictions.java:57: error: cannot find symbol 
        Prediction p = new Prediction(); 

verwendete ich das Servlet-api.jar enthalten in de Apache-Tomcat, wenn ich nicht verwenden, den gleichen Fehler gibt für javax.servlet

javac -classpath C:\apache-tomcat-9.0.0.M8\webapps\predictions\WEB-INF\classes\predictions;C:\apache-tomcat-9.0.0.M8\lib\servlet-api.jar Predictions.java 

Was mache ich falsch?

+0

Klassen kompilieren von 'classes' Ordner' Javac Prognosen \ * java' mit Einstellung. Servlet-api.jar in Klassenpfad – Sanjeev

+0

Es ist Arbeit: javac -classpath C: \ apache-tomcat-9.0.0.M8 \ lib \ servlet-api.jar Vorhersagen \ *. Java, aber warum ist es nicht möglich eins nach dem anderen? Vielen Dank! –

+0

Siehe hinzugefügt Antwort. – Sanjeev

Antwort

0

Kompilieren Sie Ihre Klassen von classes Ordner javac predictions\*.java mit der Einstellung Servlet-api.jar im Klassenpfad.

Sie können sie einzeln kompilieren auch richtige Klasse-Pfad nach dem Abbinden:

javac predictions\Prediction.java 
javac predictions\Predictions.java 

Hoffe, es hilft

Verwandte Themen