开发者

Compiling and executing through commandLine shows NoClassDefFoundError when trying to find Java package

开发者 https://www.devze.com 2023-01-01 16:21 出处:网络
I have a client/开发者_如何学编程server program that attempts to send and receive an object.

I have a client/开发者_如何学编程server program that attempts to send and receive an object.

There are three packages: server, client and shared shared contains only the Message class

I put Message.java from shared package into the same folder as calcclient package source files and calcserver package source files.

I compile using the line: javac -classpath .; (long list of client or server.java files) Message.java They can compile. Then I change directory up one level and ran with: java -classpath .; .Main

When I use Netbeans to run, the entire program works as per normal. But not if I run from command line. If its executed through command line, the program will work until it needs to use the Message object. Then it will show a NoClassDefFoundError

Am I putting the right files at the right places? How do I get the program to find shared package through command line?


The files are not in the right place. The Message class belongs to a different package so it shouldn't be living with the other classes. From http://java.sun.com/j2se/1.5.0/docs/tooldocs/findingclasses.html :

User classes are classes which build on the Java platform. To find user classes, the launcher refers to the user class path -- a list of directories, JAR archives, and ZIP archives which contain class files.

A class file has a subpath name that reflects the class's fully-qualified name. For example, if the class com.mypackage.MyClass is stored under /myclasses, then /myclasses must be in the user class path and the full path to the class file must be /myclasses/com/mypackage/MyClass.class. If the class is stored in an archive named myclasses.jar, then myclasses.jar must be in the user class path, and the class file must be stored in the archive as com/mypackage/MyClass.class.

You have a couple of options:

The best solution is to take the time to learn Ant. Netbeans projects are built with Ant, which is a really great feature of Netbeans in my book, and you can open up the build.xml in your project and find a reasonably well commented description of what Netbeans does to build your project. And really I don't think there would be many places around that run builds from the command line so learning something like Ant would be a great help.

The next level down in sophistication would be to manually build a Jar for your shared package and put it somewhere on the classpath.

The most basic approach is just to compile the java files into class files and put them in the appropriate directory reflecting the package name as explained in the quote above.


If you build your project in NetBeans, you'll see that there is a dist folder where you can find your project in binary code. After building the source code, NetBeans specifies how should you start your project from command line.

If you use this and the problem persists, you should rebuild your Message class as a library, link it to the project using NetBeans and the project should work from command line using the command specified in NetBeans.

If you want to manually compile your source files, I think the best solution is to google something like this: manually compile Java source code


Quote the classpath value if it contains spaces.

java -classpath C:\Program Files\java\...;C:\...
                          ^
                          This is what's killing you slowly.

Try it this way:

java -classpath "C:\Program Files\java\...;C:\..."
0

精彩评论

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