简体   繁体   English

Java- java.lang.NoClassDefFoundError,类路径不是问题

[英]Java- java.lang.NoClassDefFoundError, classpath not the issue

I'm writing a program for homework and running into an issue I can't seem to resolve. 我正在编写用于作业的程序,遇到了我似乎无法解决的问题。 The problem involves simulating the probabilities of a randomwalk ending on any given node (just background, not really relevant to the problem). 问题涉及模拟随机游走在任何给定节点上的概率(只是背景,与问题无关)。 I've written my own class that uses a hash map to hold node objects (UndirectedGraph and NodeEntry respectively). 我编写了自己的类,该类使用哈希映射保存节点对象(分别为UndirectedGraph和NodeEntry)。 I've also written a test harness. 我还写了一个测试工具。

Originally all these were in one file but I decided to move the UndirectedGraph and NodeEntry to a separate package...cause that seems like the right thing to do. 最初,所有这些文件都放在一个文件中,但是我决定将UndirectedGraph和NodeEntry移到单独的程序包中……原因似乎是正确的做法。 I've gotten everything fixed up so that testHarness will compile but at runtime I get the following: 我已经修复了所有问题,以便可以编译testHarness,但是在运行时我得到以下信息:

Exception in thread "main" java.lang.NoClassDefFoundError: GraphWalker/NodeEntry
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2531)
at java.lang.Class.getMethod0(Class.java:2774)
at java.lang.Class.getMethod(Class.java:1663)
at sun.launcher.LauncherHelper.getMainMethod(LauncherHelper.java:494)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:486)
Caused by: java.lang.ClassNotFoundException: GraphWalker.NodeEntry
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    ... 6 more

from looking around I find a few answers for this, primarily that the classes aren't on the classpath. 通过环顾四周,我可以找到一些答案,主要是这些类不在类路径中。 Thing is I have all these files in the same folder, and my understanding is the current folder is always on the classpath. 我将所有这些文件放在同一文件夹中,并且我的理解是当前文件夹始终位于类路径中。

here's abbreviated copies of the code: 这是该代码的缩写副本:

testHarness: testHarness:

import java.util.*;
import GraphWalker.*;

public class testHarness {

    public static void main (String args[]) {
        new testHarness();
    }


    public testHarness() {
        GraphWalker.UndirectedGraph graph = new GraphWalker.UndirectedGraph(3);
//      System.out.println(graph.containsNode(1));
        graph.addNode(1);
        graph.addNode(2);
        graph.addNode(3);

UndirectedGraph: 无向图:

package GraphWalker;
import java.util.*;

public class UndirectedGraph {

    /* 
     * Based in part on UndirectedGraph.java by Keith Schwarz (htiek@cs.stanford.edu) 
     */

    public HashMap graphMap;

    public UndirectedGraph(int numNodes) {
        this.graphMap = new HashMap(numNodes);
    }

    public void addNode (Integer nodeNum) {
        graphMap.put(nodeNum, new NodeEntry(1.0f,0.0f));
    }

NodeEntry: NodeEntry:

package GraphWalker;
import java.util.*;


public class NodeEntry {

    // four inherent values
    // credit is the current credit of the node
    // nextCredit holds the credit for the next step
    // adjList holds a list of adjacent node IDs 
    // degree holds the number of neighbors
    public Float credit;
    public Float nextCredit; 
    public ArrayList adjList;
    public Integer degree;


    public NodeEntry(Float credit, Float nextCredit) {
        this.credit = credit;
        this.nextCredit = nextCredit;
        this.adjList = new ArrayList();
        this.degree = 0;
    }


    public void addEdge(Integer neighbor) {
        this.adjList.add(neighbor);
        this.degree += 1;
    }

each class has quite a bit more code but I don't think it's relevant to the issue. 每个类都有很多代码,但我认为这与问题无关。 I'm working on Ubuntu 12.04 and trying to compile and run from both command line and using Geany (simple IDE) same behavior either way. 我正在Ubuntu 12.04上工作,尝试从两个命令行进行编译和运行,并且以任何一种方式使用Geany(简单IDE)相同的行为。

Any help? 有什么帮助吗?

Alrighty, The issue was that the package files needed to be in a separate folder (named for the package, in my case GraphWalker). 好了,问题在于,程序包文件需要放在一个单独的文件夹中(以程序包命名,在我的情况下为GraphWalker)。 I'm not entirely clear on why it compiled fine but at runtime when it went to look for the package it seems to have expected/required them to be in their own folder. 我尚不完全清楚为什么它可以编译良好,但是在运行时,当它去寻找软件包时,似乎已经期望/要求它们位于自己的文件夹中。

I wouldn't consider that a class path issue, per se, since the files being looked for were in a folder that was on the classpath, just not the folder they needed to be in. 我不会考虑类路径问题,因为要查找的文件位于类路径上的文件夹中,而不是它们所需的文件夹中。

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

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