Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
10-26-2006, 05:54 AM
|
#1
|
|
Member
Registered: Apr 2005
Location: San Jose
Distribution: Fedora 3,4- Ubuntu 6.06 to 8.10, Gentoo and Arch
Posts: 408
Rep:
|
running java on my Ubuntu 6.06 machine
I've just installed sun-java5-jre and sun-java5-jdk from synaptic and followed instruction in this site: https://help.ubuntu.com/community/Java
After doing all steps I just tried to run a simple Hello World program.
Code:
public class HelloWorld
{
public static void main(String [] args)
{
System.out.println("Hello, World");
}
}
I did javac HwlloWorld.java and made HelloWorld.class file.
After running "java HelloWorld.class" I got the following error:
Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld/class
Does anyone know what's wrong with jave on my machine?
|
|
|
|
10-26-2006, 07:27 AM
|
#2
|
|
Member
Registered: Nov 2003
Location: Bristol, UK
Distribution: Debian Lenny, Gentoo (at work)
Posts: 388
Rep:
|
Nothing's wrong
You should run "java HelloWorld" without the .class extension.
Hope this helps.
|
|
|
|
10-26-2006, 03:53 PM
|
#3
|
|
Member
Registered: Apr 2005
Location: San Jose
Distribution: Fedora 3,4- Ubuntu 6.06 to 8.10, Gentoo and Arch
Posts: 408
Original Poster
Rep:
|
I got the same error after running java HelloWorld.java 
|
|
|
|
10-26-2006, 04:06 PM
|
#4
|
|
Member
Registered: May 2006
Location: Argentina
Distribution: SuSE 10
Posts: 173
Rep:
|
I think that you need to put your HelloWorld.java in a directory which is in the CLASSPATH. You should check what your CLASSPATH variable says.
Cheers!
|
|
|
|
10-26-2006, 04:59 PM
|
#5
|
|
Member
Registered: Nov 2003
Location: Bristol, UK
Distribution: Debian Lenny, Gentoo (at work)
Posts: 388
Rep:
|
Ah, it's not "java HelloWorld.java" either. Just "java HelloWorld"
demon_vox is right, the file does have to be in a directory in your CLASSPATH, but the current directory (i.e. ".") should always be in this. It can't hurt to check though.
|
|
|
|
10-26-2006, 06:57 PM
|
#6
|
|
Member
Registered: Apr 2005
Location: San Jose
Distribution: Fedora 3,4- Ubuntu 6.06 to 8.10, Gentoo and Arch
Posts: 408
Original Poster
Rep:
|
WOW java HelloWorld works.
Do you know why it is like this? I was told that to run java files one must make a binary file which is readable by java virtual machine and then run the program. But in this case it seems to me I can run this program like an interpreted language.
Anyway thank you very much
|
|
|
|
10-26-2006, 11:09 PM
|
#7
|
|
Moderator
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Ubuntu, OL
Posts: 9,311
|
Quote:
|
Originally Posted by mohtasham1983
WOW java HelloWorld works.
Do you know why it is like this? I was told that to run java files one must make a binary file which is readable by java virtual machine and then run the program.
|
It is indeed the case.
"java HelloWorld" is using HelloWorld.class, which is a binary (bytecode) file.
Quote:
|
But in this case it seems to me I can run this program like an interpreted language.
|
It's not the case. Java isn't interpreted.
|
|
|
|
10-27-2006, 07:16 AM
|
#8
|
|
Member
Registered: Nov 2003
Location: Bristol, UK
Distribution: Debian Lenny, Gentoo (at work)
Posts: 388
Rep:
|
Quote:
|
Do you know why it is like this?
|
It's the way Java organises binary files. If you're writing a more complex program, with several classes, you can organise the classes into groups, called packages, to help keep things more organised. A dot is used to separate package and class names, so what's happening when you run "java HelloWorld.class", is Java is looking for a class called "class", in a package called "HelloWorld". Which, of course, doesn't exist. By running "java HelloWorld", then Java will look for a class called "HelloWorld" in the default package, which is what you want.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 12:53 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|