Jimi(java):Jimi.createJimiWriter 上出现 Noclassdeffound 异常
我犹豫是否要问这个问题,但我花了很多时间弄乱环境变量,我只是无法弄清楚这一点:
我试图将小程序上显示的所有内容保存到图像文件中,经过一些研究,我认为 JIMI 是我最好的选择 (下面不是我的原创作品,而是来自 http://www.coderanch.com/t/337338/GUI/java/save-applet-image-gif-jpeg 关于我将要做什么)
import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Panel;
import java.awt.Point;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.image.ColorModel;
import java.awt.image.PixelGrabber;
import java.io.OutputStream;
import java.util.Vector;
import com.sun.jimi.core.Jimi;
import com.sun.jimi.core.JimiWriter;
public class JimiApplet extends Applet implements ActionListener {
private ImageCanvas canvas;
private TextField filename;
private Button save, clear;
public void init() {
setLayout( new BorderLayout() );
canvas = new ImageCanvas();
add( canvas, BorderLayout.CENTER );
Panel p = new Panel();
filename = new TextField();
save = new Button( "Save" );
save.addActionListener( this );
clear = new Button( "Clear" );
clear.addActionListener( this );
p.setLayout( new GridBagLayout() );
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = .70;
p.add( filename, c );
c.weightx = .15;
p.add( save, c );
c.weightx = .15;
p.add( clear, c );
add( p, BorderLayout.SOUTH );
}
public void actionPerformed(ActionEvent e) {
if ( e.getSource() == save ) {
save.setEnabled( false );
filename.setEnabled( false );
filename.setEditable( false );
Thread t = new Thread( new SaveButtonPressed() );
t.start();
} else if ( e.getSource() == clear ) {
canvas.clear();
}
}
private static class ImageCanvas extends Canvas implements MouseMotionListener {
private Vector points;
public ImageCanvas() {
points = new Vector();
addMouseMotionListener( this );
}
public void mouseDragged(MouseEvent e) {
Point p = e.getPoint();
points.add( p );
repaint();
}
public void mouseMoved(MouseEvent e) {
}
public void clear() {
points.removeAllElements();
repaint();
}
public void paint( Graphics g ) {
int size = points.size();
g.setColor( Color.black );
if ( size > 0 ) {
Point p1 = (Point)points.get( 0 );
for ( int i = 0; i < size; i++ ) {
Point p2 = (Point)points.get( i );
g.drawLine( p1.x, p1.y, p2.x, p2.y );
p1 = p2;
}
}
}
}
private class SaveButtonPressed implements Runnable {
public void run() {
Thread t = new Thread( new SaveRunnable() );
t.start();
try {
t.join();
} catch( InterruptedException x ) {
x.printStackTrace();
}
finally {
save.setEnabled( true );
filename.setEnabled( true );
filename.setEditable( true );
}
}
}
private class SaveRunnable implements Runnable {
public void run() {
int width = canvas.getSize().width;
int height = canvas.getSize().height;
ColorModel cm = canvas.getColorModel();
int[] pixels = new int[ width * height ];
Image image = canvas.createImage( width, height );
Graphics g = image.getGraphics();
canvas.paint( g );
PixelGrabber pg = new PixelGrabber( image, 0, 0, width, height, pixels, 0, width );
boolean success = false;
try {
success = pg.grabPixels();
}
catch (InterruptedException e2) {
e2.printStackTrace();
return;
}
if ( success ) {
String mimeType = "invalid";
String extension = filename.getText();
int index = extension.indexOf( '.' );
extension = extension.substring( index +1);
System.out.println(extension);
if ( extension.equalsIgnoreCase( "GIF" ) ) {
mimeType = "/image/gif";
} else if ( extension.equalsIgnoreCase( "JPG" ) ||
extension.equalsIgnoreCase( "JPEG" ) ) {
mimeType = "image/jpeg";
} else if ( extension.equalsIgnoreCase( "PNG" ) ) {
mimeType = "image/png";
}
// You can add more options here for the different image types
// you want to support.
if ( mimeType.equals( "invalid" ) ) {
System.err.println( "Could not get a valid mime type from file extension!" );
return;
} else {
try {
OutputStream imageOutput = null;
// Do something here to get an OutputStream pointing to the socket,
// URL of servlet, etc. that you are going to write your image to.
System.out.println("got here " + mimeType);
JimiWriter writer = Jimi.createJimiWriter( mimeType );
System.out.println("probably not here");
writer.setSource( image );
writer.putImage( imageOutput );
} catch ( Exception x ) {
x.printStackTrace();
return;
}
}
} else {
System.err.println( "PixelGrabber failed!" );
}
}
}
}
我可以编译此代码JIMI 在我的环境变量中。然而,在尝试保存图像时,我收到错误:
Exception in thread "Thread-5" java.lang.NoClassDefFoundError: com/sun/jimi/core/Jimi
at JimiApplet$SaveRunnable.run(JimiApplet.java:165)
at java.lang.Thread.run(Thread.java:662)
Caused by: java.lang.ClassNotFoundException: com.sun.jimi.core.Jimi
at sun.applet.AppletClassLoader.findClass(AppletClassLoader.java:211)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.applet.AppletClassLoader.loadClass(AppletClassLoader.java:144)
at java.land.ClassLoader.loadClass(ClassLoader.java:247)
... 2 more
经过研究,我发现这很可能是由于我的环境变量中没有 jimiproclasses 引起的,所以我尝试添加它们进行多种组合,花了我一个半小时才完成做我认为可行的所有组合 在 CLASSPATH 和 PATH 中,以下是我尝试过的一些操作:
C:\Users\dfreelan\Dropbox\colormap\Jimi;
C:\Users\dfreelan\Dropbox\colormap\Jimi\JimiProClasses.zip;
C:\Users\dfreelan\Dropbox\colormap\Jimi;C:\Users\dfreelan\Dropbox\colormap\Jimi\JimiProClasses.zip;
C:\Users\dfreelan\Dropbox\colormap\Jimi\src\classes
我尝试了更多组合,但我相信您可以在这里了解要点 对于上面的每个示例,我都可以编译(这表明某种形式的 Jimi 正在工作),我只是无法在运行时不收到错误的情况下执行 Jimi.createJimiWriter
非常感谢任何人的帮助。如果这看起来是一个菜鸟问题,我很抱歉,但我已经研究了这么久,而且我如此仔细地攻击它,我觉得我没有地方可以转向!
I hesitated to ask this, but i have spent numerous hours messing with environmental variables, I just cannot figure this out:
I am trying to save whatever is displayed on an applet into an image file, after some research i thought JIMI was my best bet
(below is not my original work, but a simple example from http://www.coderanch.com/t/337338/GUI/java/save-applet-image-gif-jpeg as to what i will be doing)
import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Panel;
import java.awt.Point;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.image.ColorModel;
import java.awt.image.PixelGrabber;
import java.io.OutputStream;
import java.util.Vector;
import com.sun.jimi.core.Jimi;
import com.sun.jimi.core.JimiWriter;
public class JimiApplet extends Applet implements ActionListener {
private ImageCanvas canvas;
private TextField filename;
private Button save, clear;
public void init() {
setLayout( new BorderLayout() );
canvas = new ImageCanvas();
add( canvas, BorderLayout.CENTER );
Panel p = new Panel();
filename = new TextField();
save = new Button( "Save" );
save.addActionListener( this );
clear = new Button( "Clear" );
clear.addActionListener( this );
p.setLayout( new GridBagLayout() );
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = .70;
p.add( filename, c );
c.weightx = .15;
p.add( save, c );
c.weightx = .15;
p.add( clear, c );
add( p, BorderLayout.SOUTH );
}
public void actionPerformed(ActionEvent e) {
if ( e.getSource() == save ) {
save.setEnabled( false );
filename.setEnabled( false );
filename.setEditable( false );
Thread t = new Thread( new SaveButtonPressed() );
t.start();
} else if ( e.getSource() == clear ) {
canvas.clear();
}
}
private static class ImageCanvas extends Canvas implements MouseMotionListener {
private Vector points;
public ImageCanvas() {
points = new Vector();
addMouseMotionListener( this );
}
public void mouseDragged(MouseEvent e) {
Point p = e.getPoint();
points.add( p );
repaint();
}
public void mouseMoved(MouseEvent e) {
}
public void clear() {
points.removeAllElements();
repaint();
}
public void paint( Graphics g ) {
int size = points.size();
g.setColor( Color.black );
if ( size > 0 ) {
Point p1 = (Point)points.get( 0 );
for ( int i = 0; i < size; i++ ) {
Point p2 = (Point)points.get( i );
g.drawLine( p1.x, p1.y, p2.x, p2.y );
p1 = p2;
}
}
}
}
private class SaveButtonPressed implements Runnable {
public void run() {
Thread t = new Thread( new SaveRunnable() );
t.start();
try {
t.join();
} catch( InterruptedException x ) {
x.printStackTrace();
}
finally {
save.setEnabled( true );
filename.setEnabled( true );
filename.setEditable( true );
}
}
}
private class SaveRunnable implements Runnable {
public void run() {
int width = canvas.getSize().width;
int height = canvas.getSize().height;
ColorModel cm = canvas.getColorModel();
int[] pixels = new int[ width * height ];
Image image = canvas.createImage( width, height );
Graphics g = image.getGraphics();
canvas.paint( g );
PixelGrabber pg = new PixelGrabber( image, 0, 0, width, height, pixels, 0, width );
boolean success = false;
try {
success = pg.grabPixels();
}
catch (InterruptedException e2) {
e2.printStackTrace();
return;
}
if ( success ) {
String mimeType = "invalid";
String extension = filename.getText();
int index = extension.indexOf( '.' );
extension = extension.substring( index +1);
System.out.println(extension);
if ( extension.equalsIgnoreCase( "GIF" ) ) {
mimeType = "/image/gif";
} else if ( extension.equalsIgnoreCase( "JPG" ) ||
extension.equalsIgnoreCase( "JPEG" ) ) {
mimeType = "image/jpeg";
} else if ( extension.equalsIgnoreCase( "PNG" ) ) {
mimeType = "image/png";
}
// You can add more options here for the different image types
// you want to support.
if ( mimeType.equals( "invalid" ) ) {
System.err.println( "Could not get a valid mime type from file extension!" );
return;
} else {
try {
OutputStream imageOutput = null;
// Do something here to get an OutputStream pointing to the socket,
// URL of servlet, etc. that you are going to write your image to.
System.out.println("got here " + mimeType);
JimiWriter writer = Jimi.createJimiWriter( mimeType );
System.out.println("probably not here");
writer.setSource( image );
writer.putImage( imageOutput );
} catch ( Exception x ) {
x.printStackTrace();
return;
}
}
} else {
System.err.println( "PixelGrabber failed!" );
}
}
}
}
I can compile this code with JIMI in my environmental variables. However, upon trying to save the image I get the error:
Exception in thread "Thread-5" java.lang.NoClassDefFoundError: com/sun/jimi/core/Jimi
at JimiApplet$SaveRunnable.run(JimiApplet.java:165)
at java.lang.Thread.run(Thread.java:662)
Caused by: java.lang.ClassNotFoundException: com.sun.jimi.core.Jimi
at sun.applet.AppletClassLoader.findClass(AppletClassLoader.java:211)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.applet.AppletClassLoader.loadClass(AppletClassLoader.java:144)
at java.land.ClassLoader.loadClass(ClassLoader.java:247)
... 2 more
After research i found that this is mostly likly caused by not having jimiproclasses in my environmental variables, so i tried to add them doing numerous combinations, took me an hour an a half to do all combinations i thought would work
In CLASSPATH AND in PATH, here are a few things i tried:
C:\Users\dfreelan\Dropbox\colormap\Jimi;
C:\Users\dfreelan\Dropbox\colormap\Jimi\JimiProClasses.zip;
C:\Users\dfreelan\Dropbox\colormap\Jimi;C:\Users\dfreelan\Dropbox\colormap\Jimi\JimiProClasses.zip;
C:\Users\dfreelan\Dropbox\colormap\Jimi\src\classes
I have tried more combinations, but i believe you can get the gist here
With each of the examples above, i can compile (which shows that some form of Jimi is working) i just simply cannot do Jimi.createJimiWriter without receiving error in runtime
Anybody's help is greatly appreciated. I am sorry if this seems a nooby question, but I have worked on it so long and i attacked it so meticulously i felt i had no place left to turn!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Applet 不会从 CLASSPATH 环境变量获取其类路径。有关如何设置小程序的类路径的信息,请参阅此问题。
Applets don't get their classpath from the CLASSPATH enviroment variable. See this question for info about how to set the classpath for an applet.