• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Ron McLeod
  • Tim Cooke
Sheriffs:
  • Devaka Cooray
  • paul wheaton
  • Mark Herschberg
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
  • Jj Roberts
Bartenders:
  • Carey Brown
  • salvin francis
  • Piet Souris

-cp option works only for java command but not for javac command, why? [SOLVED]

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Javac -cp option Works Only in java command(when want to run a program) but not in javac command, when i want to compile.(OS Windows 10)

Let`s say i am in cmd in Directory1 and want to compile ClassA.java which is in a package called packagea which is in Directory2 which is in Directory1. Like The Following

--Directory1
         |
         |______Directory2
                            |
                            |______packagea
                                             |
                                             |______ClassA.java

I am in Directory1 and want to Compile ClassA.java
If i go "standard" and write:  javac Directory2/packagea/ClassA.java  (it compile)
If i also write                    :  javac ./Directory2/packagea/ClassA.java  (it compile)

BUT if i write                    :  javac -cp Directory2 packagea/ClassA.java (says error: file not found packagea/ClassA.java)
Also i try                         :  javac -cp Directory2/packagea/ClassA.java (error: no source files)

Now i did the following, i just compile with javac Directory2/packagea/ClassA.java and then i try to run it standard and with -cp otpion which both works:

So i did:                          :javac Directory2/packagea/ClassA.java (and it compile)
Execute Standard             : cd Directory2 (i go in Directory2)
                                     : java packagea.ClassA (the program run properly)

Or using -cp otpion from Directory1
Step1                             : cd.. (return from Directory2 to Directory1)
Step2                             : java -cp Directory2 packagea.ClassA (it run the program using -cp option, so when i run the program, -cp otpion works, but if i want to compile like that it doesn`t work at all.)

Now the book OCP Complete Study Guide Jeanne Boyarsky and Scott Selikoff  at pag 24, Table 1.3 said -cp Option should work for both javac and java commands. But i tryed like i had explain and it worka only for java but not for javac.
I understand how to compile in the "standard way" so i can tell that i can compile a file, but i wonder why it doesn`t work with -cp option.
If someone have the patience to try it, thank you all !!  
 
Saloon Keeper
Posts: 6835
162
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch.

A path is not a classpath. A classpath is used to find class files, not source files. You would use it to point the compiler to where your compiled .class files are, not for pointing the compiler to .java files. For a simple example like this one, "-cp" is not needed.
 
Alex Damian
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Moores wrote:Welcome to the Ranch.

A path is not a classpath. A classpath is used to find class files, not source files. You would use it to point the compiler to where your compiled .class files are, not for pointing the compiler to .java files. For a simple example like this one, "-cp" is not needed.



Hmm, ok, so basically -cp can be used only for run the program and not for compile?
Becouse in this Book, says -cp can be used for "Location of classes needed to compile the program" and for "Location of classes needed to run the program".
 
Master Rancher
Posts: 4188
38
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

 -cp can be used only for run the program and not for compile?


-cp would be needed for the javac command to find the definitions for user classes.
 
Alex Damian
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:

 -cp can be used only for run the program and not for compile?


-cp would be needed for the javac command to find the definitions for user classes.



Thank you !!
 
Alex Damian
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Moores wrote:Welcome to the Ranch.

A path is not a classpath. A classpath is used to find class files, not source files. You would use it to point the compiler to where your compiled .class files are, not for pointing the compiler to .java files. For a simple example like this one, "-cp" is not needed.



Thank you !!
 
Saloon Keeper
Posts: 23263
158
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Technically, a "path" is just the directory tree used to locate a file or files. However, in the context of command execution, the "PATH" is one or more "paths" used to locate executable files. These files are native machine code files, NOT java classes.

A classpath, on the other hand, contains paths to java classes and are NOT used to locate executable non-java code.

The javac compiler uses the classpath to see the structure of classes that it will use as reference information to do its job of compiling. It will not attempt to execute these classes, just read their structure and metadata.

The java runtime program, on the other hand, can and often does locate executable classes on its classpath. Or, to be more precise, the standard classloaders use the classpath to locate the classes that they will load. The classpath is also used to locate non-class Java resource such as properties files, something commonly done by user-written application logic.
 
A magnificient life is loaded with tough challenges. En garde tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic