开发者

Include Swing when compiling with javac

开发者 https://www.devze.com 2023-02-09 20:50 出处:网络
I compile and jar the source just fine, but when I run it, it complains: java.lang.ClassNotFoundException: javax.swing.JPanel

I compile and jar the source just fine, but when I run it, it complains:

java.lang.ClassNotFoundException: javax.swing.JPanel

I guess I have to include the Swing library when compiling,开发者_如何转开发 but how do I do that?

I included every rt.jar on my system:

javac -classpath /usr/lib/jvm/java-1.5.0-gcj-4.4/jre/lib/rt.jar:/usr/lib/jvm/java-6-sun-1.6.0.20/jre/lib/rt.jar:/usr/lib/jvm/java-6-openjdk/jre/lib/rt.jar:/home/me/equinox.jar *java

Still compiles fine, still crashes when run.


It looks like you are using GCJ. It's an old project that attempted to produce a Java implementation as pure open source.

They got about halfway, but the implementation is far from perfect. These days, it's better to avoid it entirely and instead use OpenJDK (or the Oracle/Sun JDK, if open source is not a requirement).

On Ubuntu you can use update-java-alternatives to configure your system to a different Java implementation:

sudo update-java-alternatives -s java-6-openjdk

By the way, you never need to specify rt.jar explicitly on your classpath, as it's always available automatically. Also, using multiple rt.jar from different JVMs is a recipe for disaster.


If you run the code, the compilation worked fine. It is weird that you are missing this particular class as it is part of the standard library. In most Java distributions, this is in rt.jar


What is your current path to the JDK (in $PATH or %PATH%)? What is your whole path? What is your current CLASSPATH environment variable? Your problem is runtime environment related. You should not add rt.jar to your classpath. If you have a proper JVM install, then java doesn't need the location to rt.jar and tools.jar. I suspect that you have pathing issues to the JVM.

Also, what is the command line you are using to run the program?


You don't need to include rt.jar, it is included by the default classloader of the java vm.

You need to find out which version of java is the default one:

java --version

if you're running ubuntu, it will most probably be openjdk java, which is fine. If it's gcj, try to run it with openjdk or sun-jdk.

Also, show exactly how you run the jar.

0

精彩评论

暂无评论...
验证码 换一张
取 消