LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 06-22-2004, 05:38 AM   #1
Lobais
Member
 
Registered: Jan 2004
Location: Denmark
Distribution: Fedora Core 6
Posts: 224

Rep: Reputation: 30
Exception in thread "main" java.lang.NoClassDefFoundError:


OK, I've just got javac working, some sdk stuff, and now I would like java to do the same. I've compiled a small program:
Code:
public class hello
{
        public static void main(String args[])
        {
                System.out.print("Hello world");
        }
}
I have compiled it and renamed the hello.class file for just hello as I saw somewhere on google.
Then when I try to run the program:
Code:
[thomas@thomas Java]$ java hello
Exception in thread "main" java.lang.NoClassDefFoundError: hello
and nothing else happens. What does it mean?
 
Old 06-22-2004, 05:48 AM   #2
Mega Man X
LQ Guru
 
Registered: Apr 2003
Location: ~
Distribution: Ubuntu, FreeBSD, Solaris, DSL
Posts: 5,339

Rep: Reputation: 65
Hi there Lobais!!!

Well, first, you should not rename the file to hello.class. Class is a "compiled" file created by javac. So, write the code as you did, save it as hello.java then use:

javac hello.java

to compile it. java.class will be created. Use:

java hello

to run the application. A side note on your code too. Java can be really peek about System.out line. Substitute you line for this:

System.out.println("Hello world");

That should work. If it fails, reinstall Java2 SDK, it can install wrongly quite often actually

Good luck!
 
Old 06-22-2004, 05:56 AM   #3
Lobais
Member
 
Registered: Jan 2004
Location: Denmark
Distribution: Fedora Core 6
Posts: 224

Original Poster
Rep: Reputation: 30
Well I actually started just with java hello.class, but someone on google wrote that you should remove the .class stuff.
Code:
[thomas@thomas Java]$ javac hello.java
[thomas@thomas Java]$ java hello.class
Exception in thread "main" java.lang.NoClassDefFoundError: hello/class
[thomas@thomas Java]$
I installed Java as the wrote on http://www.fedorafaq.org/custom_java.html just with the sdk package. I think I have installed it more than twice already.
 
Old 06-22-2004, 05:58 AM   #4
Mega Man X
LQ Guru
 
Registered: Apr 2003
Location: ~
Distribution: Ubuntu, FreeBSD, Solaris, DSL
Posts: 5,339

Rep: Reputation: 65
yup, the error is here then:

java hello.class

After you've used "javac hello.java" to compile the application, hello.class will be created. However, to run the application, you've to type "java hello" without the extension (.class)
 
Old 06-22-2004, 06:13 AM   #5
Lobais
Member
 
Registered: Jan 2004
Location: Denmark
Distribution: Fedora Core 6
Posts: 224

Original Poster
Rep: Reputation: 30
That was the thing i didn't understood. Thank you so much.
But why doesn't this work for Anjuta ?

Code:
EXECUTING:
java /home/thomas/Java/hello
----------------------------------------------
Exception in thread "main" java.lang.NoClassDefFoundError: /home/thomas/Java/hello
 
----------------------------------------------
Program exited successfully with errcode (1)
Press the Enter key to close this terminal ...
 
Old 06-22-2004, 06:16 AM   #6
Mega Man X
LQ Guru
 
Registered: Apr 2003
Location: ~
Distribution: Ubuntu, FreeBSD, Solaris, DSL
Posts: 5,339

Rep: Reputation: 65
Hey cool . Well, I've never tried anjuta myself, but everybody says it's great . Sorry that I cannot help ya out with that . However, since we are talking about IDE's, I'd like to recommend DrJava:

http://drjava.sourceforge.net/

It's pretty nice, written in Java, open-source and great for learning Java

Cheers!
 
Old 06-22-2004, 06:21 AM   #7
Lobais
Member
 
Registered: Jan 2004
Location: Denmark
Distribution: Fedora Core 6
Posts: 224

Original Poster
Rep: Reputation: 30
OK, I'll have a look on drjava, but it's a little pretty that I doesn't have OS look
Thanks.
 
Old 06-22-2004, 06:34 AM   #8
aaa
LQ Guru
 
Registered: Jul 2003
Location: VA
Distribution: Slack 10.1
Posts: 2,194

Rep: Reputation: 47
Try 'java -classpath . Hello'
 
Old 06-22-2004, 06:40 AM   #9
Lobais
Member
 
Registered: Jan 2004
Location: Denmark
Distribution: Fedora Core 6
Posts: 224

Original Poster
Rep: Reputation: 30
Instead of the Anjuta "java $(current.full.filename)" execute command?
 
Old 06-22-2004, 07:11 AM   #10
aaa
LQ Guru
 
Registered: Jul 2003
Location: VA
Distribution: Slack 10.1
Posts: 2,194

Rep: Reputation: 47
If you want to use anjuta you will have to do something slightly different.
For 'java -classpath . Hello', make sure you're in the directory that the file's in (w/ cd).
For anjuta, you'd do something like:
java -classpath $(name.of.anjutas.variable.for.the.files.dir) $(current.full.filename)
 
Old 06-22-2004, 07:33 AM   #11
Lobais
Member
 
Registered: Jan 2004
Location: Denmark
Distribution: Fedora Core 6
Posts: 224

Original Poster
Rep: Reputation: 30
I can run the program in the terminal with:
Code:
[thomas@thomas Java]$ javac hello.java
[thomas@thomas Java]$ ls
hello.class  hello.java
[thomas@thomas Java]$ java hello
Hello world
[thomas@thomas Java]$
So no need for classpath there.

In Anjuta, what is the variable.for.the.files.dir thing?
 
Old 06-22-2004, 07:47 AM   #12
Lobais
Member
 
Registered: Jan 2004
Location: Denmark
Distribution: Fedora Core 6
Posts: 224

Original Poster
Rep: Reputation: 30
I made it. I just changed the java "$(current.full.filename)" for java "$(current.file.name)".
But say me one thing. Why can't linux run the class files without using a program? Whit compiled c files, it just runs them.
 
Old 06-22-2004, 08:02 AM   #13
aaa
LQ Guru
 
Registered: Jul 2003
Location: VA
Distribution: Slack 10.1
Posts: 2,194

Rep: Reputation: 47
Java .class files are not executables. They are the code you wrote in a sort of compressed form (it can be easily decompiled, unlike c code). The java program is needed to run them.
 
Old 06-22-2004, 08:07 AM   #14
Lobais
Member
 
Registered: Jan 2004
Location: Denmark
Distribution: Fedora Core 6
Posts: 224

Original Poster
Rep: Reputation: 30
How do I make them into binary?
 
Old 06-22-2004, 05:00 PM   #15
Mega Man X
LQ Guru
 
Registered: Apr 2003
Location: ~
Distribution: Ubuntu, FreeBSD, Solaris, DSL
Posts: 5,339

Rep: Reputation: 65
You "can't", yet. Gcc has an extension that translate java byte code into native code, called gcj. The draw back is, that project is in early stages of development and don't support any graphical interface of java, as Swing.

Also, translating your code into machine language completely kills the purpose of Java, which is portability.

No language is safe, not c/c++ either. There're decompilers for everything. The process of decompiling is known as Reverse Engineering. Take a look here for some decompilers:

http://www.free2space.com/tools/decompilers/setup.html

That said, the two very best ways of protecting your code is: copy right it or release as GPL.

If I was you, I'd more worried to learn Java efficiently (or any other language for that matter) then someone, somehow, in the future, will hack your application. If it does, just sue their pants off. It's quite funny and I bet, profitable
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Starting Java from shell script (Exception in thread "main") rolf_mueller Linux - Software 5 10-30-2004 02:11 AM
Exception in thread "main" java.lang.InternalError: Can't connect to X11 window serve paragvd Programming 4 10-05-2004 03:35 AM
Java error "Exception in thread "main" java.lang.StackOverflowError" nro Programming 1 09-04-2004 03:47 AM
Exception in thread "main" java.lang.NoClassDefFoundError: melinda_sayang Programming 2 04-27-2004 11:49 AM
Exception "NoClassDefFoundError" from java degraffenried13 Linux - Software 8 09-11-2003 11:45 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 06:36 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration