• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Compile with -cp or -classpath

 
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hmm ok I see what you mean Marc.

Thanks
 
Paper beats rock. Scissors beats tiny ad.
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic