ClassNotFoundException使用处理与Arduino工具的PApplet

时间:2015-02-20 13:48:48

标签: java eclipse processing classnotfoundexception arduino-ide

我开发了一个基本的Processing PApplet,作为Arduino IDE中的工具运行,直到Arduino 1.5.8才运行。我遇到的问题是,在Arduino 1.6.0中,一些代码被重构,这发生在Arduino方面:

  

"为了提供更好的命令行,IDE已经过重构   进入app和arduino-core这是旧核心为了避免   类之间的冲突,PApplet被移除   processing.core.PAppletprocessing.app.legacy.PApplet"

这个解释来自Arduino IDE开发人员之一。值得注意的是,processing.app.legacy.PApplet是一个(非常)精简的PApplet版本,丢弃了我需要的所有图形功能。

最初我收到了这个错误:

Uncaught exception in main method: java.lang.NoClassDefFoundError: processing/core/PApplet

将处理的core.jar放置在与eclipse导出的工具jar相同的位置修复了这个问题,但让另一个:

Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: You need to use "Import Library" to add processing.core.PGraphicsJava2D to your sketch.
    at processing.core.PApplet.makeGraphics(Unknown Source)
    at processing.core.PApplet.init(Unknown Source)

令人困惑的部分是我使用Processing的库源java文件而不是core.jar编译库来避免这个问题,但它并没有改变任何东西。

我已经浏览了PApplet的源代码,发现图形/渲染器类在运行时here被加载和实例化,如下所示:

Class<?> rendererClass =
        Thread.currentThread().getContextClassLoader().loadClass(renderer);

      Constructor<?> constructor = rendererClass.getConstructor(new Class[] { });
      PGraphics pg = (PGraphics) constructor.newInstance();

this是捕获ClassNotFoundException抛出Runtime异常的地方:

catch (ClassNotFoundException cnfe) {
//      if (cnfe.getMessage().indexOf("processing.opengl.PGraphicsOpenGL") != -1) {
//        throw new RuntimeException(openglError +
//                                   " (The library .jar file is missing.)");
//      } else {
      if (external) {
        throw new RuntimeException("You need to use \"Import Library\" " +
                                   "to add " + renderer + " to your sketch.");
      } else {
        throw new RuntimeException("The " + renderer +
                                   " renderer is not in the class path.");
      }

    }

我对java越来越熟悉,但我没有足够的经验来解决这个问题。它看起来像是一个类路径问题,但我不确定为什么会发生这种情况,以及我应该如何告诉java在哪里找到它需要加载的类。

以下是我根据IDE附带的Arduino Tool示例使用的代码测试。目前我正在从eclipse中导出jar文件(不是runnable):

/*
  Part of the Processing project - http://processing.org

  Copyright (c) 2008 Ben Fry and Casey Reas

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software Foundation,
  Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

package com.transformers.supermangletron;


import java.awt.Color;
import java.awt.Dimension;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import processing.core.PApplet;
import processing.app.Editor;
import processing.app.tools.Tool;
//import processing.app.legacy.PApplet;

/**
 * Example Tools menu entry.
 */
public class Mangler implements Tool {
  private Editor editor;

  public void init(Editor editor) {
    this.editor = editor;
  }

  private void setupSketch(){
    int w = 255;
    int h = 255;

//      PApplet ui = new PApplet();
    TestApp ui = new TestApp();

      JFrame window = new JFrame(getMenuTitle());
      window.setPreferredSize(new Dimension(w,h+20));

    window.add(ui);
      window.invalidate();
      window.pack();
      window.setVisible(true);
      window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      ui.init();

    System.out.println("setup complete");
  }

  public String getMenuTitle() {
    return "Mangle Selection";
  }


  public void run() {
      setupSketch();
  }

}

这是我尝试显示的基本测试Applet:

package com.transformers.supermangletron;

import processing.core.PApplet;

public class TestApp extends PApplet {
    public void setup(){
        size(100,100);
    }
    public void draw(){
        background((1.0f+sin(frameCount * .01f)) * 127);
    }
}

如何使用我的设置修复此ClassNotFoundException? 关于我应该仔细检查课程路径的任何提示?

1 个答案:

答案 0 :(得分:1)

如果从eclipse运行时出现此错误,则必须将库jar添加到类路径中。您可以通过右键单击项目,转到属性,然后转到Java Build Path来执行此操作。确保库文件库列在“库”选项卡下。

如果您在运行导出的jar时遇到此错误,则需要执行以下两项操作之一:

要么确保导出一个可运行的jar,其中包含嵌入在主jar中的库jar。通过右键单击项目,转到Export,然后选择&#34; runnable jar&#34;来自eclipse。从列表中。

或者,确保将类路径设置为JVM参数。您可以使用命令行中的-cp选项执行此操作。