简体   繁体   English

尝试使用ObjectDraw运行Java程序时线程“ main”错误

[英]Exception in thread “main” error when trying to run a java program with objectdraw

Primer: 底漆:

I'm starting a Java class at UCSD next week and our textbook has us download a library called objectdraw.jar which we will be using for the first two chapters. 下周我要在UCSD上开始Java类,我们的教科书上有我们下载了一个名为objectdraw.jar的库,我们将在前两章中使用该库。 I downloaded the library and placed it in a folder called java_libraries in my ~/home/dev/ directory. 我下载了该库,并将其放在~/home/dev/目录中的java_libraries文件夹中。

I am trying to run my Java program on Linux and I'm getting an error when I try to run it. 我试图在Linux上运行我的Java程序,但在尝试运行它时出现错误。 It compiles fine, but does not run. 它可以正常编译,但不能运行。

To compile, I'm issuing the following command from the directory where my TouchyWindow.java file exists ~/dev/java/ : 为了进行编译,我从TouchyWindow.java文件存在的目录~/dev/java/发出以下命令:

javac -classpath ../java_libraries/objectdraw.jar TouchyWindow.java

NOTE: It compiles without error or warning. 注意:编译时不会出现错误或警告。

To run the program, I'm issuing the following command from the directory where my TouchWindow.class file exists ~/dev/java/ : 要运行该程序,我从TouchWindow.class文件存在的目录~/dev/java/发出以下命令:

java -classpath ../java_libraries/objectdraw.jar TouchyWindow

When I try to execute the program, I get the following error: 当我尝试执行程序时,出现以下错误:

Exception in thread "main" java.lang.NoClassDefFoundError: /home/fhaddad78/dev/java/TouchyWindow
Caused by: java.lang.ClassNotFoundException: .home.fhaddad78.dev.java.TouchyWindow
        at java.net...
        at java.security...
        at java.net...
        at java.lang...
        at sun.misc...
        at java.lang...
Could not find the main class: /home/fhaddad78/dev/java/TouchyWindow. Program will exit.

About my system: 关于我的系统:

I'm doing this on Gentoo Linux using the Iceatea Java SDK. 我正在使用Iceatea Java SDK在Gentoo Linux上执行此操作。 I mention this because I'm not sure if that could cause the problem. 我提到这一点是因为我不确定这是否会导致问题。

I'm new to Java and not really sure what this means since the file it can not find is in the path it's saying it can't find it in. 我是Java的新手,因此不确定它的含义,因为找不到的文件位于其无法找到的路径中。

UPDATE To help illustrate the program, I will past the source code as it's only a few lines. 更新为了帮助说明该程序,我将跳过源代码,因为它只有几行。

// TouchyWindow.java

import objectdraw.*;
import java.awt.*;

public class TouchyWindow extends WindowController {
  public void onMousePress(Location point) {
    new Text("I'm Touched", 40, 50, canvas);
  }
  public void onMouseRelease(Location point) {
    canvas.clear();
  }
}

The objectdraw.jar library file handles whatever may appear to be missing in my source file. objectdraw.jar库文件处理了源文件中可能缺少的所有内容。

UPDATE For curiosity sake, I booted into Windows, installed the Java SDK and Eclipse. 更新出于好奇,我启动Windows,安装了Java SDK和Eclipse。 Created a new project, used the same code as above, and all compiled and worked without any problems. 创建了一个新项目,使用与上述相同的代码,所有文件均已编译并正常工作。 Does that help at all with possible guidance as to the problem? 这对于问题的可能指导是否有任何帮助?

UPDATE regarding objectdraw.jar 关于objectdraw.jar的更新

Could this be my problem? 这可能是我的问题吗?

The following was take from the textbook's website. 以下内容取自教科书的网站。

Using objectdraw with applications rather than applets There is a simple way of using objectdraw with applications that is new with version 1.1 and later of the objectdraw library. 将objectdraw与应用程序而不是applet一起使用有一种简单的方法可以将objectdraw与应用程序一起使用,这是1.1版及更高版本的objectdraw库中的新增功能。 Under these version of objectdraw, you can run extensions of WindowController (or Controller) as applications by using a new method named startController. 在这些版本的objectdraw下,您可以使用名为startController的新方法将WindowController(或Controller)的扩展作为应用程序运行。 This method is included in the Controller class (and thus is inherited by WindowController). 此方法包含在Controller类中(因此由WindowController继承)。

To turn what would have been an applet into a program that can also be run as an application, simply add the following method to your class that extends Controller or WindowController: 要将原来的小程序转换为也可以作为应用程序运行的程序,只需将以下方法添加到扩展Controller或WindowController的类中:

public static void main(String[] args) { new MyClassName().startController(400,400); 公共静态无效main(String [] args){new MyClassName()。startController(400,400); } }

In the above, MyClassName is a placeholder for the name of the class extending Controller, while the parameters (400,400) specify the size of the window desired. 在上面,MyClassName是扩展Controller的类的名称的占位符,而参数(400,400)指定所需窗口的大小。

You need to add the directory where the .class file exits to class path. 您需要将.class文件退出的目录添加到类路径。

The output of javac goes to a directory, let us call it BUILD_DIR. javac的输出进入一个目录,让我们将其称为BUILD_DIR。 Add this build dir to class path 将此构建目录添加到类路径

java -classpath ../java_libraries/objectdraw.jar:$BUILD_DIR TouchyWindow

If the TouchWindow has some package you need to add the folder where the package begin. 如果TouchWindow有某些软件包,则需要在该软件包开始的位置添加文件夹。 The class name in that case will have to be packagename.packagename.ClassName 在这种情况下,类名必须为packagename.packagename.ClassName

The library expects you to use applet. 该库希望您使用applet。 It says add following to run as program 它说添加以下内容以程序运行

public static void main(String[] args) { 
   new MyClassName().startController(400,400); 
} 
  1. Leave off the .jar file from your classpath (java will accept directories and file all .jar files in there automatically) 从您的类路径中.jar文件(java将接受目录并自动将所有.jar文件归档在其中)
  2. Fully qualify your class - without the package, java will assume it's at the top level (ie no package) 完全限定您的课程-没有软件包,java将认为它是顶级的(即没有软件包)

Try this: 尝试这个:

java -cp ../java_libraries com.mypackage.TouchyWindow

暂无
暂无

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

相关问题 线程“main”中的异常当尝试运行TestNG类时,Eclipse中出现错误 - Exception in thread “main” Error in Eclipse when trying to run TestNG class 尝试使用 Java 在 Selenium 中运行测试用例时,线程“主”java.lang.NullPointerException 出现异常 - Exception in thread "main" java.lang.NullPointerException when trying to run a test case in Selenium with Java 线程“main”中的异常 java.util.InputMismatchException .. 当我尝试运行程序时会发生这种情况 - Exception in thread “main” java.util.InputMismatchException.. that's what happens when i try to run the program (PySpark)路径错误:线程“ main”中的异常java.io.ioexception无法运行程序“ python” - (PySpark) Pathing error: exception in thread “main” java.io.ioexception cannot run program “python” 线程“main”中的异常java.io.IOException:无法运行程序“”:错误=2,没有这样的文件或目录 - Exception in thread "main" java.io.IOException: Cannot run program "": error=2, No such a file or directory 运行Web抓取程序时出现“线程“ main”中的异常“ java.lang.NullPointerException”错误 - “Exception in thread ”main“ java.lang.NullPointerException” error when running web scraper program 编译程序时出现错误(线程“ main”中的异常java.lang.ArrayIndexOutOfBoundsException) - I get the Error when i compile my program (Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException) 尝试在列主要表单java中填充2D数组时,线程主错误中的异常 - Exception in thread main error when trying to fill in a 2D array in column major form java mapreduce程序中的错误:线程“ main”中的异常java.lang.VerifyError - error in mapreduce program : Exception in thread “main” java.lang.VerifyError 简单程序中的“线程“主”java.lang.Error 中的异常” - "Exception in thread "main" java.lang.Error" in a simple program
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM