This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Compile with -cp or -classpath

 
Santiago Bravo
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rekha Srinath
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Santiago Bravo
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hmm ok I see what you mean Marc.

Thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic