jar文件在运行时无法正常工作

时间:2013-04-16 13:39:54

标签: java jar manifest noclassdeffounderror classnotfoundexception

我不认为在创建jar文件时我做错了什么,但这里是:

jar cvfm JarTest.jar manifest.txt Main.class Main$ThisPanel.class Main$ThisPanel$1.class Main$ThisPanel$2.class BallJump.class in cmd

问题是我无法运行jar文件,当我尝试从cmd运行它时,我收到此错误消息:

Exception in thread "main" java.lang.NoClassDefFoundError: javax/madia/j3d/Canvas3D
    at Main.<init>(Main.java:24)
    at Main.main(Main.java:17)
Caused by: java.lang.ClassNotFoundException: javax.media.j3d.Canvas3D
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknow Sourc)
    ... 2 more

这是我的java代码:

package Game;

import com.sun.j3d.utils.geometry.Sphere;
import com.sun.j3d.utils.universe.SimpleUniverse;
import java.awt.*;
import java.awt.event.*;
import javax.media.j3d.*;
import javax.swing.*;
import javax.vecmath.*;

public class Main extends JFrame implements ActionListener{
    private Image image;
    private Timer timer;
    private BallJump ball;
    private ThisPanel panel;
    private boolean inMenu = true;

    public static void main(String[] args){
        new Main();
    }

    public Main(){
        setFocusable(true);
        ImageIcon ii = new ImageIcon("Ball Jump.png");
        panel = new ThisPanel();
        ball = new BallJump();
        image = ii.getImage();
        timer = new Timer(5,this);
        timer.start();
        setSize(600,600);
        add(panel);
        setVisible(true);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public void actionPerformed(ActionEvent e){
        setVisible(true);
        repaint();
    }

    private class ThisPanel extends JPanel{

        public ThisPanel(){
            InputMap im = getInputMap(WHEN_IN_FOCUSED_WINDOW);
            ActionMap am = getActionMap();

            im.put(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0, false), "pressed");
            im.put(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0, true), "released");

            am.put("pressed", new AbstractAction() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if(inMenu == true){
                        inMenu = false;
                        remove(panel);
                        ball.setAlive(true);
                        add(ball);            
                        setVisible(true); 
                    }

                    if (ball.isAlive() != true){ 
                        setSize(600,600);                     
                        setVisible(true);
                        repaint();
                        remove(ball);
                        ball = new BallJump();
                        add(ball); 
                        repaint();
                    } 
                }
            });

            am.put("released", new AbstractAction() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if(inMenu == true){
                        inMenu = false;
                        remove(panel);
                        ball.setAlive(true);
                        add(ball);            
                        setVisible(true); 
                    }

                    if (ball.isAlive() != true){ 
                        remove(ball);
                        ball = new BallJump();
                        add(ball);
                        setVisible(true);
                        repaint();    
                    }
                }
            });

            setFocusable(true);
            requestFocusInWindow();        
        }

        public Dimension getPreferredSize() {
            return new Dimension(600, 600);
        }

        @Override
        public void paintComponent(Graphics g){
            super.paintComponent(g);
            if(inMenu == true){         
                Graphics2D g2D = (Graphics2D) g;
                g2D.drawImage(image, 0, 0, this);
            }    
        }
    }
}

class BallJump extends JPanel implements ActionListener,KeyListener{
    private TransformGroup objTrans,objTrans2, objTrans3, objTrans4, objTrans5, objTrans6, objTrans7;
    private Transform3D trans = new Transform3D();
    private BranchGroup objRoot = new BranchGroup();
    private BranchGroup objRoot2 = new BranchGroup();
    private BranchGroup objRoot3 = new BranchGroup();
    private BranchGroup objRoot4 = new BranchGroup();
    private BranchGroup objRoot5 = new BranchGroup();
    private BranchGroup objRoot6 = new BranchGroup();
    private BranchGroup objRoot7 = new BranchGroup();
    private SimpleUniverse u;
    private Canvas3D c;
    private BranchGroup scene, scene2, scene3, scene4, scene5, scene6, scene7;
    private Sphere sphere, sphere2, sphere3, sphere4, sphere5, sphere6, sphere7;
    private float height = 0.0f, sign = 1.0f, xloc = 0.0f;
    private float height2 = 0.0f, sign2 = -1.0f;
    private float rightX, right, rightDx, leftX, left, leftDx, rightX2, leftX2;
    private boolean isAlive = true;
    private JLabel scoreLabel;
    private JLabel label;
    private int score = -400;
    private Timer timer;
    private boolean isEndscreen = true;


    public boolean isAlive(){
        return isAlive;
    }

    public void setAlive(boolean isAlive){
        this.isAlive = isAlive;
    }

    public BranchGroup createSceneGraph(){
        Color3f ambientColourRSphere = new Color3f(0.6f,0.0f,0.0f);
        Color3f emissiveColourRSphere = new Color3f(0.0f,0.0f,0.0f);
        Color3f diffuseColourRSphere = new Color3f(0.8f,0.0f,0.4f);
        Color3f specularColourRSphere = new Color3f(0.3f,0.0f,0.0f);
        float shininessRSphere = 20.0f;
        Appearance redSphereApp = new Appearance();
        redSphereApp.setMaterial(new Material(ambientColourRSphere,emissiveColourRSphere,
                             diffuseColourRSphere,specularColourRSphere,shininessRSphere));
        objRoot.setCapability(BranchGroup.ALLOW_DETACH);
        objTrans = new TransformGroup();
        objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        objRoot.addChild(objTrans);
        sphere = new Sphere(0.15f,redSphereApp);
        objTrans = new TransformGroup();
        objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        Transform3D pos1 = new Transform3D();
        pos1.setTranslation(new Vector3f(0.0f,0.0f,0.0f));
        objTrans.setTransform(pos1);
        objTrans.addChild(sphere);
        objRoot.addChild(objTrans);
        BoundingSphere bounds = new BoundingSphere
                (new Point3d(0.0,0.0,0.0),100.0);
        Color3f light1Color = new Color3f(0.2f,0.2f,1.0f);
        Vector3f light1Direction = new Vector3f(+4.0f,-7.0f,-12.0f);
        DirectionalLight light1 = new DirectionalLight
                (light1Color,light1Direction);
        light1.setInfluencingBounds(bounds);
        objRoot.addChild(light1);
        Color3f ambientColor = new Color3f(1.0f,1.0f,1.0f);
        AmbientLight ambientLightNode = new AmbientLight(ambientColor);
        ambientLightNode.setInfluencingBounds(bounds);
        objRoot.addChild(ambientLightNode); 
        Color3f bgColor = new Color3f(0.0f, 0.2f, 1.0f);
        Background bg = new Background(bgColor);
        bg.setApplicationBounds(bounds);
        objRoot.addChild(bg);
        return objRoot;
    }

    public BranchGroup createSceneGraph2(){
        objRoot2.setCapability(BranchGroup.ALLOW_DETACH);
        objTrans2 = new TransformGroup();
        objTrans2.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        objRoot2.addChild(objTrans2);
        sphere2 = new Sphere(0.18f);
        objTrans2 = new TransformGroup();
        objTrans2.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        Transform3D pos1 = new Transform3D();
        pos1.setTranslation(new Vector3f(0.0f,0.0f,0.0f));
        objTrans2.setTransform(pos1);
        objTrans2.addChild(sphere2);
        objRoot2.addChild(objTrans2);
        return objRoot2;
    }

    public BranchGroup createSceneGraph3(){
        objRoot3.setCapability(BranchGroup.ALLOW_DETACH);
        objTrans3 = new TransformGroup();
        objTrans3.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        objRoot3.addChild(objTrans3);
        sphere3 = new Sphere(0.18f);
        objTrans3 = new TransformGroup();
        objTrans3.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        Transform3D pos1 = new Transform3D();
        pos1.setTranslation(new Vector3f(0.0f,0.0f,0.0f));
        objTrans3.setTransform(pos1);
        objTrans3.addChild(sphere3);
        objRoot3.addChild(objTrans3);
        return objRoot3;
    }

    public BranchGroup createSceneGraph4(){
        objRoot4.setCapability(BranchGroup.ALLOW_DETACH);
        objTrans4 = new TransformGroup();
        objTrans4.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        objRoot4.addChild(objTrans4);
        sphere4 = new Sphere(0.45f);
        objTrans4 = new TransformGroup();
        objTrans4.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        Transform3D pos1 = new Transform3D();
        pos1.setTranslation(new Vector3f(0.0f,0.0f,0.0f));
        objTrans4.setTransform(pos1);
        objTrans4.addChild(sphere4);
        objRoot4.addChild(objTrans4);
        return objRoot4;
    }

    public BranchGroup createSceneGraph5(){
        objRoot5.setCapability(BranchGroup.ALLOW_DETACH);
        objTrans5 = new TransformGroup();
        objTrans5.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        objRoot5.addChild(objTrans5);
        sphere5 = new Sphere(0.45f);
        objTrans5 = new TransformGroup();
        objTrans5.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        Transform3D pos1 = new Transform3D();
        pos1.setTranslation(new Vector3f(0.0f,0.0f,0.0f));
        objTrans5.setTransform(pos1);
        objTrans5.addChild(sphere5);
        objRoot5.addChild(objTrans5);
        return objRoot5;
    }

    public BranchGroup createSceneGraph6(){
        objRoot6.setCapability(BranchGroup.ALLOW_DETACH);
        objTrans6 = new TransformGroup();
        objTrans6.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        objRoot6.addChild(objTrans6);
        sphere6 = new Sphere(0.18f);
        objTrans6 = new TransformGroup();
        objTrans6.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        Transform3D pos1 = new Transform3D();
        pos1.setTranslation(new Vector3f(0.0f,0.0f,0.0f));
        objTrans6.setTransform(pos1);
        objTrans6.addChild(sphere6);
        objRoot6.addChild(objTrans6);
        return objRoot6;
    }

    public BranchGroup createSceneGraph7(){
        objRoot7.setCapability(BranchGroup.ALLOW_DETACH);
        objTrans7 = new TransformGroup();
        objTrans7.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        objRoot7.addChild(objTrans7);
        sphere7 = new Sphere(0.18f);
        objTrans7 = new TransformGroup();
        objTrans7.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        Transform3D pos1 = new Transform3D();
        pos1.setTranslation(new Vector3f(0.0f,0.0f,0.0f));
        objTrans7.setTransform(pos1);
        objTrans7.addChild(sphere7);
        objRoot7.addChild(objTrans7);
        return objRoot7;
    }

    BallJump(){
        setFocusable(true);
        setLayout(new BorderLayout());
        setVisible(true);
        GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
        c = new Canvas3D(config);
        Color color = new Color(0.0f, 0.2f, 1.0f);
        setBackground(color);
        scoreLabel = new JLabel("Score: 0");     
        scoreLabel.setBackground(color);
        add(BorderLayout.PAGE_START,scoreLabel);
        add("Center",c);
        c.addKeyListener(this);
        c.setSize(585,560);
        timer = new Timer(60,this);
        timer.start();
        scene = createSceneGraph();
        scene2 = createSceneGraph2();
        scene3 = createSceneGraph3();
        scene4 = createSceneGraph4();
        scene5 = createSceneGraph5();
        scene6 = createSceneGraph6();
        scene7 = createSceneGraph7();
        u = new SimpleUniverse(c);
        u.getViewingPlatform().setNominalViewingTransform();
        u.addBranchGraph(scene);
        u.addBranchGraph(scene2);
        u.addBranchGraph(scene3);
        u.addBranchGraph(scene4);
        u.addBranchGraph(scene5);
        u.addBranchGraph(scene6);
        u.addBranchGraph(scene7);
        float randomSpawn1 = (float) Math.random() * -3.7f + -4.1f;
        float randomSpawn2 = (float) Math.random() * 2.5f + 3.1f; 
        float randomSpawn3 = (float) Math.random() * -2.7f + -5.1f;
        float randomSpawn4 = (float) Math.random() * 2.5f + 4.1f; 
        float randomSpawn5 = (float) Math.random() * -5.7f + -15.1f;
        float randomSpawn6 = (float) Math.random() * 5.7f + 18.1f; 
        rightX = randomSpawn2;
        right = randomSpawn4;
        leftX = randomSpawn1;
        left = randomSpawn3;
        rightX2 = randomSpawn6;
        leftX2 = randomSpawn5;
        rightDx = -.05f;
        leftDx = .05f;  
    }

    public void keyPressed(KeyEvent e){
        if(e.getKeyChar() == 'd'|e.getKeyChar() == 'D'|e.getKeyCode() == e.VK_RIGHT){
            xloc = xloc + .1f;
        }

        if(e.getKeyChar() == 'a'|e.getKeyChar() == 'A'|e.getKeyCode() == e.VK_LEFT){
            xloc = xloc - .1f;
        }
    }    

    public void keyReleased(KeyEvent e){

    }

    public void keyTyped(KeyEvent e){

    }

    public void actionPerformed(ActionEvent e){
        height += .1f * sign;
        if(Math.abs(height * 2) >= 1)
            sign = -1.0f * sign;
        height2 += .1f * sign2;
        if(Math.abs(height2 * 2) >= 1)
            sign2 = -1.0f * sign2;
        Vector3d vec;
        int thing = 1;
        int thing2 = 2;
        if(height < -.4f){
          vec = new Vector3d(1.0,.7,1.0);
        }else{
          vec = new Vector3d(1.0,1.0,1.0);
          thing = 2;
        }
        trans.setScale(vec);
        trans.setTranslation(new Vector3f(xloc,height - .15f,0.0f));
        objTrans.setTransform(trans);
        if(height < -.10f){
          vec = new Vector3d(1.0,1.0,1.0);
          thing2 = 1;
        }
        if(height < -.4f){
          trans.setScale(new Vector3d(1.0,1.0,1.0));
        }
        if(score >= 1000){
            rightDx = -.06f;
            leftDx = .06f; 
        }
        if(score >= 2000){
            rightDx = -.07f;
            leftDx = .07f; 
        }
        if(score >= 4000){
            rightDx = -.08f;
            leftDx = .08f; 
        }
        if(score >= 5000){
            rightDx = -.09f;
            leftDx = .09f; 
        }
        if(score >= 10000){
            rightDx = -.1f;
            leftDx = .1f; 
        }
        if(score >= 15000){
            rightDx = -.12f;
            leftDx = .12f; 
        }
        Vector3f vector = new Vector3f(rightX += rightDx,-.7f,0.0f);
        trans.setTranslation(vector);
        objTrans2.setTransform(trans);
        Vector3f vector2 = new Vector3f(leftX += leftDx,-.7f,0.0f);
        trans.setTranslation(vector2);
        objTrans3.setTransform(trans);
        Vector3f vector3 = new Vector3f(rightX2 += rightDx,-.4f,0.0f);
        trans.setTranslation(vector3);
        objTrans4.setTransform(trans);
        Vector3f vector4 = new Vector3f(leftX2 += leftDx,-.4f,0.0f);
        trans.setTranslation(vector4);
        objTrans5.setTransform(trans);
        Vector3f vector5 = new Vector3f(right += rightDx,-.7f,0.0f);
        trans.setTranslation(vector5);
        objTrans6.setTransform(trans);
        Vector3f vector6 = new Vector3f(left += leftDx,-.7f,0.0f);
        trans.setTranslation(vector6);
        objTrans7.setTransform(trans);
        float leftXDistance = vector2.x - xloc - .22f;
        float leftXDistancez = vector6.x - xloc - .22f;
        float leftXDistance2 = vector4.x - xloc - .22f;
        float yDistance2 = -.4f - height;
        float xDistance2 = vector3.x - xloc + .22f;
        float xDistance = vector.x - xloc + .22f;
        float yDistance = -.7f - height;
        float xDistance3 = vector5.x - xloc + .22f;
        double leftdistance = Math.sqrt((leftXDistance * leftXDistance) + (yDistance * yDistance));
        double leftdistancez = Math.sqrt((leftXDistancez * leftXDistancez) + (yDistance * yDistance));
        double leftdistance2 = Math.sqrt((leftXDistance2 * leftXDistance2) + (yDistance2 * yDistance2));
        double distance2 = Math.sqrt((xDistance2 * xDistance2) + (yDistance2 * yDistance2));
        double distance = Math.sqrt((xDistance * xDistance) + (yDistance * yDistance));
        double distance3 = Math.sqrt((xDistance3 * xDistance3) + (yDistance * yDistance));
        if(distance < vec.x / 2 + .09f){
            if(thing == 2){     
                score += 100;
                scoreLabel.setText("Score: " + score);
                float randomSpawn = (float) Math.random() * 2.5f + 3.1f;
                rightX = randomSpawn;
                trans.setTranslation(vector);
                objTrans2.setTransform(trans);
            }
            if(thing == 1){
                objRoot.detach();
                setAlive(false);
                isAlive = false;
                isEndscreen = false;
            }
        }
        if(leftdistance < vec.x / 2 + .09f){
            if(thing == 2){      
                score += 100;
                scoreLabel.setText("Score: " + score);
                float randomSpawn = (float) Math.random() * -2.7f + -3.3f;
                leftX = randomSpawn;
                trans.setTranslation(vector);
                objTrans3.setTransform(trans);
            }
            if(thing == 1){
                objRoot.detach();
                setAlive(false);
                isAlive = false;
                isEndscreen = false;
            }
        }
        if(leftdistancez < vec.x / 2 + .09f){
            if(thing == 2){  
                score += 100;
                scoreLabel.setText("Score: " + score);
                float randomSpawn = (float) Math.random() * -2.5f + -3.1f;
                left = randomSpawn;
                trans.setTranslation(vector);
                objTrans7.setTransform(trans);
            }
            if(thing == 1){
                objRoot.detach();
                setAlive(false);
                isAlive = false;
                isEndscreen = false;
            }
        }
        if(leftdistance2 < vec.x / 2 + .09f){
            if(thing == 2){ 
                score += 200;
                scoreLabel.setText("Score: " + score);
                float randomSpawn = (float) Math.random() * -4.5f + -15.1f;
                leftX2 = randomSpawn;
                trans.setTranslation(vector);
                objTrans5.setTransform(trans);
            }
            if(thing == 1){
                objRoot.detach();
                setAlive(false);
                isAlive = false;
                isEndscreen = false;
            }
        }
        if(distance2 < vec.x / 2 + .09f){
            if(thing == 2){ 
                score += 200;
                scoreLabel.setText("Score: " + score);
                float randomSpawn = (float) Math.random() * 4.5f + 15.1f;
                rightX2 = randomSpawn;
                trans.setTranslation(vector);
                objTrans4.setTransform(trans);
            }
            if(thing == 1){
                objRoot.detach();
                setAlive(false);
                isAlive = false;
                isEndscreen = false;
            }
        }
        if(distance3 < vec.x / 2 + .09f){
            if(thing == 2){
                score += 100;
                scoreLabel.setText("Score: " + score);
                float randomSpawn = (float) Math.random() * 2.5f + 3.1f;
                right = randomSpawn;
                trans.setTranslation(vector5);
                objTrans6.setTransform(trans);
            }
            if(thing == 1){
                objRoot.detach();
                isAlive = false;
                isEndscreen = false;
            }
        }

        if(rightX < -1.5f){
            float randomSpawn = (float) Math.random() * 1.5f + 2.1f;
            rightX = randomSpawn;
            trans.setTranslation(vector);
            objTrans2.setTransform(trans);
        }
        if(leftX > 1.5f){
            float randomSpawn = (float) Math.random() * -1.5f + -2.1f;
            leftX = randomSpawn;
            trans.setTranslation(vector2);
            objTrans3.setTransform(trans);
        }
        if(right < -1.5f){
            float randomSpawn = (float) Math.random() * 1.5f + 2.1f;
            right = randomSpawn;
            trans.setTranslation(vector);
            objTrans2.setTransform(trans);
        }
        if(left > 1.5f){
            float randomSpawn = (float) Math.random() * -1.5f + -2.1f;
            left = randomSpawn;
            trans.setTranslation(vector2);
            objTrans3.setTransform(trans);
        }
        if(rightX2 < -1.5f){
            float randomSpawn = (float) Math.random() * 1.5f + 2.1f;
            rightX2 = randomSpawn;
            trans.setTranslation(vector3);
            objTrans4.setTransform(trans);
        }
        if(leftX2 > 1.5f){
            float randomSpawn = (float) Math.random() * -1.5f + -2.1f;
            leftX2 = randomSpawn;
            trans.setTranslation(vector4);
            objTrans5.setTransform(trans);
        }
        if(xloc > .8f){
            xloc = .8f;
        }
        if(xloc < -.8f){
            xloc = -.8f;
        }
        if(!isAlive && !isEndscreen){
            endScreen();
            timer.stop();
        }
    }

    public void endScreen(){
        isEndscreen = true;
        remove(scoreLabel);
        remove(c);
        setVisible(true);
        setLayout(new BorderLayout(1,1));
        Font font = new Font("Helvatica",Font.BOLD,24);
        label = new JLabel("      Your Score: " + score);  
        JLabel label2 = new JLabel("Press Space To Restart");
        label2.setFont(font);
        label.setFont(font);
        setBackground(Color.white);    
        add(BorderLayout.NORTH,label);
        add(BorderLayout.CENTER,label2);
        repaint();
    }
}

1 个答案:

答案 0 :(得分:0)

java.lang.NoClassDefFoundError: javax/madia/j3d/Canvas3D

需要将Java 3D放在应用程序的运行时类路径上。