Skip to main content
gamedev.net
🔒 Locked

Problem running a simple Java-application

Started by john13th Jan 19, 2007 at 3:33 AM 3 replies 700+ views
Original Post
john13th
john13th
I have just started to learn Java and the first program I do is a "Hello world!"-application. I have then no problems of compiling it but when I run the class-file by writing >java Hello, I get this error: java.lang.NoClassDefFoundError: helloworldapp/Hello Exception in thread "main" What does that mean and what do you do about it? This is how I wrote the code: import javax.swing.*; public class Hello { public static void main (String[] args) { JOptionPane.showMessageDialog(null, "Hello World!"); System.exit(0); } }
erissian
erissian
Well, I'm not sure how your directories are laid out, but I presume that it expects the class to be somewhere that it's not.

Try: $ java -classpath helloworldapp Hello
or: $ java -classpath . Hello

Whichever one is most appropriate to your structure.
We''re sorry, but you don''t have the clearance to read this post. Please exit your browser at this time. (Code 23)
john13th
john13th
Thanks it worked now, especially with the second commando. But where on the computer does it expect that the class-files should be placed? And how do you change the run commando in Net Beans development environment so that it uses
>java -classpath . class-file commando instead of the regular?
erissian
erissian
I can't help you with NetBeans, but the classpath is dependent on the structure of the files when you compile the class. In your case, it was probably expecting them to be in a subdirectory called helloworldapp/, but you were trying to execute it from that directory, instead of it's parent.

More or less, it expects you to run it from the same directory you build it.
We''re sorry, but you don''t have the clearance to read this post. Please exit your browser at this time. (Code 23)
gommo
gommo
You will need to read up about 'packages'

Java uses packages as an extra level of encapsulation. You'll probably find that by default Netbeans placed your code into a helloworldapp package.

As a result to run that file you'd run it from outside the hellowordapp directory and type
java hellowordapp.Hello

i.e.
A Class (SomeClass) in package com.somedomain.somepackage

resides in a path
com/somedomain/somepage/SomeClass.java

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.