Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

packages and CLASSPATH

 
simon fletcher
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The textbook I am using says that with Java 7 the CLASSPATH doesn't have to be defined. Is this true? I haven't had much luck importing any packages I have created myself. This makes me think I should have CLASSPATH defined, and that I am misinterpretting what the textbook is saying.

Also, the Oracle Java Tutorials say that you can store source and class files in seperate directories. Their example is:

<path_one>\sources\com\example\graphics\Rectangle.java
<path_two>\classes\com\example\graphics\Rectangle.class

I am using Windows. Let's say that I keep all my Java source and class files in 'My Documents\Java' folder. The path I see at the top of the My Documents/Java window is C:\Documents and Settings\SKF\My Documents\Java. Does this mean, using the Oracle Java Tutorials example, that <path_one> and <path two> on my machine would both be C:\Documents and Settings\SKF\My Documents\Java?

If I organized my souce and class files in seperate folders, as suggested in the tutorials and using their Rectangle example, the paths to each would be:

C:\Documents and Settings\SKF\My Documents\Java\sources\com\example\graphics
C:\Documents and Settings\SKF\My Documents\Java\classes\com\example\graphics

Assuming I have created the 'sources', 'classes', 'com', 'example', and 'graphics' folders, if I am going to use the CLASSPATH environment variable, should it be defined (in the edit window of the Environment Variable window) as

C:\Documents and Settings\SKF\My Documents\Java\sources;C:\Documents and Settings\SKF\My Documents\Java\classes

And then, whenever I create other classes that import Rectangle (no matter where the source files for these classes I create are when they get compiled) should all I need to include in the import statement be

and I can assume the compiler will find the Rectangle class?

If I have a source file called Shapes.java in a folder like C:\Documents and Settings\SKF\My Documents\Java\sources\com\example\graphics and it imports Rectangle (which is in C:\Documents and Settings\SKF\My Documents\Java\classes\com\example\graphics) should the Shapes.java file be able to access the Rectangle.class file when it gets compiled?
 
Henry Wong
author
Marshal
Pie
Posts: 21385
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
simon fletcher wrote:The textbook I am using says that with Java 7 the CLASSPATH doesn't have to be defined. Is this true? I haven't had much luck importing any packages I have created myself. This makes me think I should have CLASSPATH defined, and that I am misinterpretting what the textbook is saying.


Well, yeah, you don't have to define a CLASSPATH variable, but that is not specific to Java 7. Even older versions of Java allowed usage without the CLASSPATH environment variable assigned. If you don't have the environment variable assigned, you can set its value as parameters to the java and javac commands. And if you don't want to do that, you can always locate your files so that it will work -- since the default is to have the current directory as the classpath.

Henry
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You really only need a classpath when you start using external .jar files or similar. By then, you will be writing big applications. Since the .jars will be different for each application, each application will require a different classpath, so set it with the -cp flag, or in the manifest file when you write your own .jars.
 
Anand Damodaran
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
simon fletcher wrote:The textbook I am using says that with Java 7 the CLASSPATH doesn't have to be defined. Is this true? I haven't had much luck importing any packages I have created myself. This makes me think I should have CLASSPATH defined, and that I am misinterpretting what the textbook is saying.


Well, yeah, you don't have to define a CLASSPATH variable, but that is not specific to Java 7. Even older versions of Java allowed usage without the CLASSPATH environment variable assigned. If you don't have the environment variable assigned, you can set its value as parameters to the java and javac commands. And if you don't want to do that, you can always locate your files so that it will work -- since the default is to have the current directory as the classpath.

Henry




Hi Henry,



Compiled: project/src/$ javac -d ../build Sum.java //successfully compiled

project/src/$ export CLASSPATH=/home/project/build;

project/src/$ javac -d ../build Test.java //successfully compiled

But, I cant run the Test class. why it so?

I have got the error: java.lang.NoClassDefFoundError. Test


please help me how to run this program...

-With respect,
Anand
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What instruction are you using to execute Test? You need to give its fully‑qualified name.
 
Anand Damodaran
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:What instruction are you using to execute Test? You need to give its fully‑qualified name.


Thanks Ritchie,

Now, i gave fully‑qualified name and run the program successfully..


Once again thank you very much..


Regards,
Anand
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You’re welcome

We all had that sort of problem when we started using packages.
 
simon fletcher
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No offense but did someone attach their own question to my question?

Also, could anyone answer the second part?

Thanks
 
Henry Wong
author
Marshal
Pie
Posts: 21385
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
simon fletcher wrote:No offense but did someone attach their own question to my question?

Also, could anyone answer the second part?


Not exactly sure -- I guess I can take another look... In the meantime, maybe you can elaborate again?

[EDIT: I guess your topic was hijacked ... Next time, can you report it earlier? We can't really do much a day after the fact -- and an answer already provided. ]

[EDIT: Not sure about your second question, but I assume it was related to javac, and answered it below.]

Henry
 
Henry Wong
author
Marshal
Pie
Posts: 21385
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
simon fletcher wrote:
Also, the Oracle Java Tutorials say that you can store source and class files in seperate directories. Their example is:

<path_one>\sources\com\example\graphics\Rectangle.java
<path_two>\classes\com\example\graphics\Rectangle.class



I am assuming that you are referring to "javac" -- in regards to reading source files and *writing* of class files. To read class files, obviously, the CLASSPATH environment variable (or "-classpath" option) is used.

With javac, to specify a source root, use the "-sourcepath" option; and to specify where to write class files, use the "-d" option.

Henry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic