aspose file tools*
The moose likes Beginning Java and the fly likes packages and CLASSPATH Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "packages and CLASSPATH" Watch "packages and CLASSPATH" New topic
Author

packages and CLASSPATH

simon fletcher
Ranch Hand

Joined: Aug 04, 2012
Posts: 50
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
Sheriff

Joined: Sep 28, 2004
Posts: 18139
    
  39

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36590
    
  16
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

Joined: Oct 04, 2012
Posts: 21
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

Joined: Oct 13, 2005
Posts: 36590
    
  16
What instruction are you using to execute Test? You need to give its fully‑qualified name.
Anand Damodaran
Greenhorn

Joined: Oct 04, 2012
Posts: 21
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

Joined: Oct 13, 2005
Posts: 36590
    
  16
You’re welcome

We all had that sort of problem when we started using packages.
simon fletcher
Ranch Hand

Joined: Aug 04, 2012
Posts: 50
No offense but did someone attach their own question to my question?

Also, could anyone answer the second part?

Thanks
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18139
    
  39

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
Sheriff

Joined: Sep 28, 2004
Posts: 18139
    
  39

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
 
wood burning stoves
 
subject: packages and CLASSPATH
 
Similar Threads
move from a position to a new position with animation
Groovy vs Java Closures
SCEA Part 2 - Basic Questions
Still fuzzy on the Expando class - have any good examples of its use?