eclipse java.lang.NoSuchMethodError: Graph: method <init>()V not found

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16704957/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 20:34:18  来源:igfitidea点击:

java.lang.NoSuchMethodError: Graph: method <init>()V not found

javalinuxeclipsecommand-line

提问by user2411854

I'm getting a confusing error in eclipse but not on our main server from the command line linux account. All code is in the main src directory, in eclipse. Code compiles at command line but produces this error in Eclipse on my Mac OS X laptop:

我在 eclipse 中遇到了一个令人困惑的错误,但在我们的主服务器上却没有从命令行 linux 帐户收到。所有代码都在主 src 目录中,在 eclipse 中。代码在命令行编译,但在 Mac OS X 笔记本电脑上的 Eclipse 中产生此错误:

Exception in thread "main" java.lang.NoSuchMethodError: Graph: method <init>()V not found
    at Lab17.main(Lab17.java:11)

The code

编码

Lab17.java

Lab17.java

import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Scanner;
public class Lab17 {

// Lab17 first attempt at creating a graph

    public static void main(String[] args) throws Exception {

        Graph myGraph = new Graph();

        URLConnection conn = null;
        try {
            URL url = new URL("http://csc.mendocino.edu/~jbergamini/222data/flights/flights");
            conn = url.openConnection();
        } catch (IOException e) {
            System.out.println("Unable to open Flights file");
            System.exit(1);
        }
        Scanner s = null;
        try {
            s = new Scanner(conn.getInputStream());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        while (s.hasNext()) {
            String src = s.next();
            String dst = s.next();
            s.next();
            double cost = s.nextDouble();
            //System.out.println(src+" "+dst+" "+cost);
            myGraph.addEdge(src, dst, cost);
        }

        System.out.println(myGraph.breadthFirst("Austin", "Washington"));    
        System.out.println(myGraph.depthFirst("LosAngeles", "Washington"));
        System.out.println(myGraph.breadthFirst("LosAngeles", "Washington"));
        System.out.println(myGraph.depthFirst("Washington", "LosAngeles"));
        System.out.println(myGraph.breadthFirst("Washington", "LosAngeles"));

    }

}

Graph.java

图.java

import java.util.LinkedList;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.TreeMap;

public class Graph {

    private TreeMap<String, Vertex> vertexMap;

    /** Creates a new, empty graph */
    public Graph() {
        // create vertexMap for Graph
        vertexMap = new TreeMap<String, Vertex>();

    }

    /**
     * Adds an edge
     * @param src the source vertex
     * @param dst the destination vertex
     * @param cost the weight of the edge
     */
    public void addEdge(String src, String dst, double cost) {
        //System.out.println(src+" "+dst+" "+cost+" in Graph()");
        // check to see if there is a src vertex
        if(vertexMap.get(src) == null) {
            // if src Vertex does not exist create Vertex for src
            vertexMap.put(src, (new Vertex(src)));
            //System.out.println(vertexMap.get(src).toString());
        }

        // check to see if there is a dst Vertex
        if(vertexMap.get(dst) == null) {
            // if dst Vertex does not exist create Vertex for dst
            vertexMap.put(dst, (new Vertex(dst)));
            //System.out.println(vertexMap.get(dst).toString());
        }

        // populate the dst and cost for Edge on the src Vertex vertexMap element 
        Vertex srdVertex = vertexMap.get(src);
        Vertex dstVertex = vertexMap.get(dst);
        Edge sEdge = new Edge(dstVertex, cost);
        srdVertex.addEdge(sEdge);
    }

    /** Clears/empties the graph */
    public void clear() {
        vertexMap = new TreeMap<String,Vertex>();
    }

    /**
     * Traverses, depth-first, from src to dst, and prints the vertex names in the order visited.
     * @param src the source vertex
     * @param dst the destination vertex
     * @return whether a path exists from src to dst
     */
    public boolean depthFirst(String src, String dst) {
        System.out.println("Depth-first from "+src+" to "+dst);
        for(Map.Entry<String,Vertex> entry: vertexMap.entrySet()) {
            String key = entry.getKey();
            Vertex thisVertex = entry.getValue();

            System.out.println(key + " => " + thisVertex.toString());
        }
        return false;
    }

    /**
     * Traverses, breadth-first, from src to dst, and prints the vertex names in the order visited
     * @param src the source vertex
     * @param dst the destination vertex
     * @return whether a path exists from src to dst
     */
    public boolean breadthFirst(String src, String dst) {
        System.out.println("Breadth-first from "+src+" to "+dst);
        // find starting vertex in vertexMap
        Vertex start = vertexMap.get(src);
        LinkedList<Vertex> vertexQue = new LinkedList<Vertex>();
        LinkedList<Vertex> visitedV = new LinkedList<Vertex>();
        // check it for null
        if( start == null) {
            throw new NoSuchElementException(" Start vertex not found");
        }

        // create a Queue for searching through vertex edges
        //Queue<Vertex> q = new Queue<Vertex>();
        vertexQue.add( start );
        start.dest = 0;
        boolean found = false;
        while( !vertexQue.isEmpty() && !found) {
            Vertex v = vertexQue.removeLast();
            if( v.toString() == dst) {
                // print queue
                found = true;
            }
            else if(!visitedV.contains(v)){
                // put all the adj vertex's into the queue
                for( Edge e: v.getEdges() ) {
                    Vertex w = e.getDst();
                    vertexQue.add( w );
                }
            }
            // add v to visitedV linked list

            if(!visitedV.contains(v)){
                visitedV.add(v);
            }
        }

        System.out.print("[");
        for(int i=0; i < visitedV.size(); i++) {
            System.out.print(visitedV.get(i)+", ");
        }
        System.out.println("]");

        /*forVertex> entry: vertexMap.entrySet()) {
            String key = entry.getKey();
            Vertex thisVertex = entry.getValue();

            System.out.println(key + " => " + thisVertex.toString());

            for(Edge e : thisVertex.getEdges() ){
                System.out.print(e.toString());
            }
            System.out.println();
            System.out.println("All Edges Evaluated");
        }*/
        return false;
    }
}

Vertex.java

顶点.java

import java.util.Set;
import java.util.TreeSet;

public class Vertex {

  private String name;
  private TreeSet<Edge> adj;
  public double dest;

  /**
   * Creates a new vertex
   * @param name the name of the vertex
   */
  public Vertex(String name) {
    this.name = name;
    adj = new TreeSet<Edge>();
  }

  public TreeSet<Edge> getEdges() {
      return this.adj;
  }

  /**
   * Returns the set of all edges starting from this vertex.
   * Set shall be ordered with respect to the names of the destination vertices.
   */
  public Set<Edge> allAdjacent() {
      Set<Edge> vertexSet = adj;

      return null;
  }

  public String toString() {

    return name;
  }

  public void addEdge(Edge e) {

      this.adj.add(e);
  }
}

public class Edge implements Comparable<Edge> {

  private Vertex dst;
  private double cost;

  /** Creates a new edge with an associated cost
   * @param dst the destination vertex
   * @param cost the weight of the edge
   */
  public Edge(Vertex dst, double cost) {
    this.dst = dst;
    this.cost = cost;
  }

  public Vertex getDst() {
      return dst;
  }

  @Override
  public int compareTo(Edge o)
  {
      if(o == null)
          return -1;
      if(this == null)
          return -1;
      if( this.dst.toString().compareTo( ((Edge)o).dst.toString() ) == 0 )
          return 0;
      else if ( this.dst.toString().compareTo( ((Edge)o).dst.toString() ) < 0 )
          return 1;
      else 
          return -1;
  }

  public String toString() {
        String theEdgeS = this.dst.toString()+" "+Double.toString(cost);
        return theEdgeS;
      }
}

Not sure why Eclipse environment is treating the code differently than the command-line environment is. The class's are a work in progress for a class. I'm only trying to solve what seems like an Eclipse bug.

不知道为什么 Eclipse 环境对待代码的方式与命令行环境不同。班级是班级正在进行的工作。我只是想解决似乎是 Eclipse 错误的问题。

回答by Jayan

It looks like the eclipse has an old version of Graph.class. You should try cleaning the project : See question Function of Project > Clean in Eclipse

看起来 eclipse 有一个旧版本的Graph.class. 您应该尝试清理项目:请参阅Eclipse 中的项目功能 > 清理问题

回答by Shailesh Pratapwar

Code compiles at command line but produces this error in Eclipse

代码在命令行编译,但在 Eclipse 中产生此错误

Probable solutions : 1) Make sure the eclipse is using the same code as you use via command line. 2) Make a clean build inside eclipse because The case might be like it still referring the old class files regardless the thing that the source has been changed.

可能的解决方案:1) 确保 Eclipse 使用的代码与您通过命令行使用的代码相同。2)在 eclipse 中进行干净的构建,因为这种情况可能就像它仍然引用旧的类文件,而不管源是否已更改。

回答by Hot Licks

When you're running, Graph.class is not found in the classpath.

运行时,在类路径中找不到 Graph.class。