gpt4 book ai didi

java - ClassNotFoundException 使用Processing's PApplet 和 Arduino 工具

转载 作者:太空宇宙 更新时间:2023-11-04 06:11:46 33 4
gpt4 key购买 nike

我开发了一个基本的Processing PApplet 作为 Arduino IDE 中的工具运行在 Arduino 1.5.8 之前都运行良好。我遇到的问题是,在 Arduino 1.6.0 中,一些代码被重构,这发生在 Arduino 端:

" In order to provide a better command line, IDE has been refactored into app and arduino-core which is the old core In order to avoid conflicts between classes, PApplet was moved from processing.core.PApplet to processing.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 的地方:

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 工具示例的代码测试。目前我正在从 eclipse 导出 jar 文件(不可运行):

/*
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();
}

}

这是我试图显示的基本测试小程序:

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?关于我应该仔细检查类路径的任何提示?

最佳答案

如果从 Eclipse 运行时遇到此错误,则必须将库 jar 添加到类路径中。您可以通过右键单击项目,转到属性,然后转到 Java 构建路径来完成此操作。确保库 jar 列在“库”选项卡下。

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

请确保导出可运行的 jar,并将库 jar 嵌入到主 jar 中。通过右键单击该项目,转到“导出”,然后从列表中选择“可运行的 jar”,可以从 Eclipse 中执行此操作。

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

关于java - ClassNotFoundException 使用Processing's PApplet 和 Arduino 工具,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28630515/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com