aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Compile with -cp or -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 » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Compile with -cp or -classpath" Watch "Compile with -cp or -classpath" New topic
Author

Compile with -cp or -classpath

Santiago Bravo
Ranch Hand

Joined: Jul 25, 2008
Posts: 226
Im currently in the folder called Java. I want to compile and run the class red.java which is in the package redPackage (redPackage.red)

This is the directory structure:

/ (root)
|
|--Java
|
|-- packages
|
|--redPackage
|
|--red.java




I can compile the file as:

javac Java/packages/readPackage/red.java

and run it as:

java -cp Java/packages readPackage.red

But how do I compile with the -cp or -classpath flag? I have tried the following but they give the error file not found:

javac -cp Java/packages/readPackage red.java

javac -cp Java/packages/readPackage/ red.java


thanks in advance


Santiago
My Path to SCJP Certification My Path to SCWCD Certification
Rekha Srinath
Ranch Hand

Joined: Sep 13, 2008
Posts: 178
The forward slash (the initial slash) to indicate that you are starting from the root of the file system is missing. Could that be the reason?
[ November 10, 2008: Message edited by: Rekha Srinath ]
Santiago Bravo
Ranch Hand

Joined: Jul 25, 2008
Posts: 226
sorry should have been more clear. There is a folder above the Java folder so the forward slash is not needed before this folder as its not the root
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Santiago Bravo:
...I can compile the file as:

javac Java/packages/readPackage/red.java

and run it as:

java -cp Java/packages readPackage.red

But how do I compile with the -cp or -classpath flag? ...

Note that when a class is declared to be part of a package, the qualified name of the class is packageName.ClassName, and its location (classpath) is the parent directory of "packageName." This is why you run your packaged class using...

java -cp Java/packages readPackage.red

Specifying a classpath for compilation is no different.

If your file is compiling correctly without an explicit classpath, then you do not need to specify one. (Note that without an explicit classpath, Java's default is to look in the current directory.) Otherwise, you need to ask yourself: What classes outside of Java's core API are required for your source file to compile? And where are these? It looks to me like they would probably be under Java/packages.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Santiago Bravo
Ranch Hand

Joined: Jul 25, 2008
Posts: 226
hmm ok I see what you mean Marc.

Thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Compile with -cp or -classpath
 
Similar Threads
Pckage Error Plz Help
package javax.servlet does not exist!!!
Head First Java Frustration!
How to set classpath in java?
Can't import user-defined packages on Windows