How to run the Jar well?
2007-01-09 23:59
411 查看
This problem has puzzled me for nearly three days...
As I don't know even this kind of thing would happen at the first place...
About how to display the picture, if i write the related codes like this:
aquariumImage = Toolkit.getDefaultToolkit().getImage("bubbles.gif");
it can be run well in the eclipse but in the jar.
so I change it into this by adding URL :
URL address = getClass().getResource("bubbles.gif");
aquariumImage = Toolkit.getDefaultToolkit().getImage(address);
it then works perfectly.
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.net.URL;
public class Test extends Frame
...{
private static final long serialVersionUID = 1L;
static Image aquariumImage;
public Test()
...{
addWindowListener(new WindowAdapter()
...{
public void windowClosing(WindowEvent e)
...{
dispose();
System.exit(0);
}
});
}
public static void main(String[] args)
...{
Test test = new Test();
test.go();
test.setSize(600, 435);
test.setVisible(true);
Graphics g = test.getGraphics();
while (!g.drawImage(aquariumImage, 0, 0, test));
}
public void go()
...{
URL address = getClass().getResource("bubbles.gif");
aquariumImage = Toolkit.getDefaultToolkit().getImage(address);
}
}
As I don't know even this kind of thing would happen at the first place...
About how to display the picture, if i write the related codes like this:
aquariumImage = Toolkit.getDefaultToolkit().getImage("bubbles.gif");
it can be run well in the eclipse but in the jar.
so I change it into this by adding URL :
URL address = getClass().getResource("bubbles.gif");
aquariumImage = Toolkit.getDefaultToolkit().getImage(address);
it then works perfectly.
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.net.URL;
public class Test extends Frame
...{
private static final long serialVersionUID = 1L;
static Image aquariumImage;
public Test()
...{
addWindowListener(new WindowAdapter()
...{
public void windowClosing(WindowEvent e)
...{
dispose();
System.exit(0);
}
});
}
public static void main(String[] args)
...{
Test test = new Test();
test.go();
test.setSize(600, 435);
test.setVisible(true);
Graphics g = test.getGraphics();
while (!g.drawImage(aquariumImage, 0, 0, test));
}
public void go()
...{
URL address = getClass().getResource("bubbles.gif");
aquariumImage = Toolkit.getDefaultToolkit().getImage(address);
}
}
相关文章推荐
- How to run the multiple tests with different weight Groovy way
- Please read “Security” section of the manual to find out how to run mysqld as root!错误解决(转)
- How to link with the correct C Run-Time (CRT) library
- HOW TO RUN YOUR WEB SERVER AND MQTT WEBSOCKETS BROKER ON THE SAME PORT
- [Yii Framework] How to run cron job (or use command) in the server with yii framework
- mysql多实例配置下,用脚本启动mysql时,出现Please read "Security" section of the manual to find out how to run mysqld as root!
- How to change the default run level of a RedHat 9.0 or Fedora Core Linux system
- Please read "Security" section of the manual to find out how to run mysqld as root!错误解决
- HOW TO RUN YOUR WEB SERVER AND MQTT WEBSOCKETS BROKER ON THE SAME PORT
- How to run the qualcomm AR samples on Android
- How to link with the correct C Run-Time (CRT) library
- You need to run build with JDK or have tools.jar on the classpath.
- PHPFarm - How to run multiple versions of PHP on the same computer
- Ex2010-19 How to Run the Exchange Management Shell EMS in PowerShell
- How to execute the jar
- How to fix “X: user not authorized to run the X server, aborting.”? -摘自网络
- How to Run Successful Projects III: The Silver Bullet (3rd Edition)
- [spring Boot] how to package and run Spring Boot application as a JAR file
- [ERROR] Fatal error: Please read "Security" section of the manual to find out how to run mysqld as root!
- How to Run a .Jar Java File