PDA

View Full Version : Executing java bytecode. Probably problem with CLASSPATH.


docmccoy
9th August 2011, 09:06 AM
Hi people,

I'm reading beautiful book "Thinkning in Java". I use Fedora Exlipse to handle the code, it works perfectly.

While later then I decided to learn howto compile without IDE in order to understand how packages work. I have read the book explanations and sure I'm familiar with creating and classes and packages as series of classes.

So I have done the following:

cd Documents/Java\ Development/ // go into development directory
mkdir ByHandLibrary // create folder for my project (or library)
cd ByHandLibrary
mkdir core // create folder for new package
cd core
create MainPoint.java file with the following text:
package core;

public class MainPoint {
public static void main(String[] args) {
System.out.println("It works!");
}
}
java MainPoint.java // compile source code
export CLASSPATH=/home/dave/Documents/Java\ Development/ByHandLibrary/ (is it okay?)
java MainPoint // run the program (MainPoint.class file has appeared due to the previous statement)
And boom:
[dave@heaven core]$ java MainPoint
Exception in thread "main" java.lang.NoClassDefFoundError: MainPoint
Caused by: java.lang.ClassNotFoundException: MainPoint
at java.net.URLClassLoader$1.run(URLClassLoader.java: 217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.j ava:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:3 21)
at sun.misc.Launcher$AppClassLoader.loadClass(Launche r.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:2 66)
Could not find the main class: MainPoint. Program will exit.



Thank you for any advice indeed!

---------- Post added at 11:06 AM ---------- Previous post was at 10:59 AM ----------

[dave@heaven core]$ java -classpath /home/dave/Documents/Java\ Development/ByHandLibrary/core/ MainPoint
does not help.

Jamwa
9th August 2011, 09:33 AM
I see an error on yout point 7:

java MainPoint.java // compile source code

Java source is compiled ising javac and not java

Now to run your code, you won't need to set the class path; just navigate to /home/dave/Documents/Java\ Development/ByHandLibrary

then run


java core.MainPoint

docmccoy
9th August 2011, 09:49 AM

Yeah, I just mistyped. Sure javac, but as I said it compiles well. The problem is (found right now) that I have to specify package name like:
java core.MainPoint
and then it works!

Fine, I added few more packages and classes and was able to connect them without errors. Now I see how to do this by hand. Really nice!

Could someone please answer my following questions:

Do you know a good tutorial on creating plugins in Eclipse? Soon I will be exploring this topic.
Am I right by guessing that ant tool (similar to C++ make files) is served for speedup compiling process ? For instance, now I have up to 10 java classes which I recompile each time if the code has been changed. What if the library expands to more than 50 classes within 5-10 packages? Then it will be obviously tedious to compile them by hand. Thus handy ant comes into a game. Is it true?


Thank you.