“无法找到主要类别” [英] "Could not find the main class"

查看:138
本文介绍了“无法找到主要类别”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从命令promopt运行示例Java应用程序,但是我收到以下错误:

I'm trying to run a sample Java application from the command promopt but I'm getting the following error:

Exception in thread "main" java.lang.NoClassDefFoundError: com/badlogic/gdx/helloworld/HelloWorldDesktop
Caused by: java.lang.ClassNotFoundException: com.badlogic.gdx.helloworld.HelloWorldDesktop
        at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: com.badlogic.gdx.helloworld.HelloWorldDesktop.  Program will exit.

我用来尝试运行这个应用程序的命令是:

The command I'm using to try and run this app is:

java -cp .;gdx.jar;gdx-backend-jogl.jar com.badlogic.gdx.helloworld.HelloWorldDesktop

所有相关文件都在当前工作目录中(.java,.class和.jar文件)

Where all relevant files are in the current working directory (.java, .class and .jar files)

我用来构建.class文件的命令如下(有2个.java文件):

The command I used to build the .class files was as follows (there are 2 .java files):

javac -cp gdx.jar;gdx-backend-jogl.jar HelloWorld.java HelloWorldDesktop.java

同样,这是从同一个工作目录运行的 - HelloWorldDesktop.java 的内容是(或多或少):

Again this was run from the same working directory - The contents of HelloWorldDesktop.java is (more or less):

package com.badlogic.gdx.helloworld;

public class HelloWorldDesktop {
    public static void main (String[] argv) {
        // Application
    }
}

我正在尝试学习Java作为C#开发人员,所以我有很强的编程概念背景,整个java工具链是目前让我很困惑。该异常表示无法找到类 HelloWorldDesktop ,但据我所知,我有正确的名称,并且我添加了正确的.jar文件到类路径,所以Java应该能够加载这个类。

I'm attempting to learn Java as a C# developer, so wheras I have a strong background in programming concepts the whole java toolchain is currently completely confusing me. The exception indicates that the class HelloWorldDesktop couldn't be found, but as far as I can tell I've got the correct name and I've added the correct .jar files to the class path and so Java should be able to load this class.

为什么找不到 HelloWorldDesktop

推荐答案

对 - 问题是你有 HelloWorldDesktop.class 在当前目录中,而它应该在com / badlogic / gdx / helloworld中

Right - the problem is that you've got HelloWorldDesktop.class in the current directory, whereas it should be in com/badlogic/gdx/helloworld

你可以使用javac命令解决这个问题 - 只需使用 -d。告诉它要对待。作为输出的包根目录。

You can fix this with the javac command - just use -d . to tell it to treat "." as the package root directory for output.

通常你会希望按包来组织你的源代码,但对于这个你好世界测试它可能不值得。

Normally you would want to also organize your source code by package, but for this "hello world" test it may not be worth it.

这篇关于“无法找到主要类别”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆