File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Package

 
munna next
Ranch Hand
Posts: 46
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have just started learning JAVA and I have few basic questions about Packages.

Here I am explaining my problem.

1. I created a Folder c:\john
2. I have created my simple HelloWorld Application like below

class finaltest{
public static void main (String args[]){

System.out.println("Hello World");
}
}
3. I have Save the file finaltest.java in to the Directory c:\John
4. I have compiled this file from the command prompt like

c:\john\javac finaltest.java
5. It is compiled and gave me the class File.
6. The class File I ran like

c:\john\java finaltest

which gave me the output.

Up to here fine.

1. I wanted to learn packages. So I modified my source like

package finalpkg;

class finaltest{
public static void main (String args[]){

System.out.println("Hello World");
}
}

2. I saved this file with the same name i.e, finaltest.java.
3. I created a sub directory "Final" under c:\john. So I have c:\john\final
4. I moved the Java source file finaltest.java from c:\john to c:\john\final.
5. From the command Prompt I compiled like c:\john\final\javac finaltest.java
6. It's compiled with out any error
7. When I run this like c:\john\final\java finaltest
I am getting the following error

Exception in thread "main" java.lang.NoClassDefFoundError: finaltest (wrong name
: finalpkg/finaltest)
at java.lang.ClassLoader.defineClass0(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)

Can any body pl explain why I am getting this error ?

Do I need to create Sub Directory with the Packge name and need to place the souce file ?

Is there something I am missing in Classpath settings ?

I have set the classpath in the Environmental setting in the system like
c:\john\final (But no Use)
I even try like c:\john (But still no use)

Please excuse me for any typo.

Thanks for your time on this.
 
Louie van Bommel
Ranch Hand
Posts: 76
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Save yourself a whole lot of trouble and try

java -d . Finaltest.java

But that's assuming you follow these steps:

Create a subfolder called finalpkg in the current folder.
Create a source file Finaltest.java with "package finalpkg"
java -d . Finaltest.java

This compiles to Finaltest.class and puts it in the subfolder finalpkg.

Run it with
java finalpkg.Finaltest (from the current folder)
[ November 03, 2004: Message edited by: Louie van Bommel ]
 
Louie van Bommel
Ranch Hand
Posts: 76
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My previous post holds true and I recommend to do it that way rather than the following. However, to specifically answer your questions in your situation:


4. I moved the Java source file finaltest.java from c:\john to c:\john\final.
5. From the command Prompt I compiled like c:\john\final\javac finaltest.java


Mistake in 4 is moving the source. Leave the source in the same place. I assume in 5 you mean that you typed javac c:\john\final\finaltest.java. In reality it doesn't matter where the heck the source is so you might as well leave it where it was. And compile it there too. You COULD do the following.

4. Leave source in c:\John
5. from command prompt javac finaltest.java
5B. move the COMPILED file finaltest.class into the finalpkg subfolder (this is what some java books say to do, but my previous post has an easier way)

7. When I run this like c:\john\final\java finaltest
I am getting ... error


What you need to do is from the parent folder you need to run it, and use dots, not slashes. So from the folder ABOVE the finalpkg folder, type:

java finalpkg.finaltest


Do I need to create Sub Directory with the Packge name and need to place the souce file ?
Is there something I am missing in Classpath settings ?

I always create a subdirectory, but I think javac -d . will create one for you. Try it. However you shouldn't place the source file in that directory. Compile it in the normal directory, and move the COMPILED file to the subdirectory. (or see previous post and save the trouble)

And forget about the classpath. You don't need to modify the classpath to work with java packages.
 
Arshad Ahamed
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
munna, your mistake is, you have created package as "package finalpkg"
whereas, you have created folder as "/final" , though the class will compile, but cannot run. - file should exists under folder name which must follow the same package name, specified in the source.
 
Arshad Ahamed
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
munna next as you are beginner, a small correction message from Louie, he specified as
"java -d . Finaltest.java" , what he means is "javac -d . Finaltest.java"

note - "javac" - just for your clarification.
 
munna next
Ranch Hand
Posts: 46
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the Postings. It's working for me.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic