This week's book giveaway is in the Spring forum.
We're giving away four copies of Pro Spring MVC with WebFlux: Web Development in Spring Framework 5 and Spring Boot 2 and have Marten Deinum & Iuliana Cosmina on-line!
See this thread for details.
Win a copy of Pro Spring MVC with WebFlux: Web Development in Spring Framework 5 and Spring Boot 2 this week in the Spring forum!
  • 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
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

When does the javac Command Require the -cp Option/Flag for Specifying a .class file

 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have been trying, without success, to imagine a scenario in which the javac command may require the -cp option/flag for specifying .class files that are required for some code compilation to be successfully completed. I will be much obliged if someone can assist me with coming up with such a scenario, please. Thanks in advance!
 
Saloon Keeper
Posts: 12994
281
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The javac command needs it when your application depends on another library. You use it to specify the directory or JAR file of each dependency.

In my example, dependency2 is a folder containing class files. Some maintainers release their libraries as a loose collection of class files. It never really happens, but it's possible.
 
Saloon Keeper
Posts: 23695
161
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
Let's say I've defined a single class that acts as a Comma-separated (CSV) File parser. It's general purpose, so I'd like to use it as a "mini library". For consistency's sake, I'd be best off putting it in a JAR file, but if I wanted to, I could just keep it as a loose class file. You'd need to put it on your compiler classpath to be able to use it when compiling apps that read CSVs. And you'd need to put it on the runtime classpath when you executed your apps, since compiling doesn't merge input classpath files (previously-compiled classes) into the classes being output.
 
Nyeng Gyang
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, gentlemen, for your responses to my inquiry.

It is quite interesting and weird that I am now getting the correct results of using the -cp option/flag. I was motivated to ask this question because, when I used this option/flag many times before asking the question, the compiler kept issuing me an error informing me that it was unable to find a class whose .class file I had quite clearly and (I believe correctly) specified, using the -cp option/flag, that the compiler should find in a directory that I am sure I placed this .class file in.

Despite recalling that I used the option/flag correctly, I suppose that it is possible this recollection is wrong, i.e., I did something wrong without realizing that I did so.
 
Marshal
Posts: 26603
81
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nyeng Gyang wrote:I suppose that it is possible this recollection is wrong, i.e., I did something wrong without realizing that I did so.



It's easy to do such a thing. Most programmers do that on a daily or weekly basis, I would guess. I do it regularly. Such events are called "bugs" and it's a normal part of the programmer's job to find and fix them.
 
Ranch Foreman
Posts: 117
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
my two cents:

To me the question itself is already a bit off: If taken word by word you ask when to use an option for supplying a path to specify a single file. To that interpretation my first reply would just be: Never.
A bit more extended variant: Never, because you specify a path, not a file.
Also: Keep in mind that packages still have to be respected. A class called somepackage.SomeClass would have to be placed in .../lib/somepackage/SomeClass.class while setting the classpath to .../lib. Setting the classpath wrongly to .../lib/somepackage will lead to ClassNotFound because there's no .../lib/somepackage/somepackage/SomeClass.class.
Also also: Different class loaders and their respective paths has to be considered: There's not just one ClassLoader but there're in fact several which load different data from different locations. One if the reasons why one wasn't able to boundle BouncyCastle with Android apps because Android already comes with BouncyCastle, although a badly crippled one. Hence the SpongyCastle came into existence just to provide a full correctly working implementation in a different namespace sheme to avoid name conflicts. As an example: Try to create and place a class in the java.lang package. You maybe able to compile it - but you will never be able to load it. This is caused by the java.lang package is sealed. This means no additional class can be loaded into it after its initial bootstrap load. It's part of Javas security concept so one just can't inject additional classes into the SE-api packages to break package protected access restrictions.
 
Nyeng Gyang
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matthew Bendford wrote:If taken word by word you ask when to use an option for supplying a path to specify a single file.


I am sure that, in my question above, I did not ask this.
 
Tim Holloway
Saloon Keeper
Posts: 23695
161
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matthew Bendford wrote:
Also: Keep in mind that packages still have to be respected. A class called somepackage.SomeClass would have to be placed in .../lib/somepackage/SomeClass.class while setting the classpath to .../lib. Setting the classpath wrongly to .../lib/somepackage will lead to ClassNotFound because there's no .../lib/somepackage/somepackage/SomeClass.class.



Repeated for emphasis!
 
Matthew Bendford
Ranch Foreman
Posts: 117
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I had to rephrase this a couple of time to keep it at a sane level - so it rather belongs into a [spoiler]-tag. Unfortunately this forum doesn't support this feature.

Nyeng Gyang wrote:

Matthew Bendford wrote:If taken word by word you ask when to use an option for supplying a path to specify a single file.


I am sure that, in my question above, I did not ask this.


Well, let's dismantle the base phrase / the title of this thread:

"javac option '-cp'" - "-cp", or its long variant: "-classpath" already has it in it: "path". So, just by its very name this option clearly requires a path.
"specifying a .class file" - well, to me this quite literally translates into my local language "specifying ONE single file".
And by my personal IT-knowledge: Although in posix "anything is a file" - a path is a directory which contains child nodes like other directories, regular files or even some other stuff.

Without any intention of personal fronting your name looks be have asian origins to me, somewhere south between India/China and Australia. I'm from Germany - so quite literally from the opposite site of the world. And hence it's quite possible our two translations to and from English are not match up with eachother ... but as written: TO ME you're asking "When should I put a path to one specific file into an option which expects a path to a directory?" - and hence without any half-a-second of thinking about it my first response when woken up at 2:30 am: "Never! It's the wrong 'type' of expected path.".

In addition to that you may recall to may have used -cp in the past but still got issues with it. Which leads to two possible options: Either pointing to the wrong directoy and missing the required package structure or by pointing to individual files rather than to the directories they're in. Or in short: Still getting errors when using -cp looks very strong you used it wrong. By the way you phrased your question and the title of this topic I highly suspect the latter one: Pointing to individual files rather than the directories.
 
Nyeng Gyang
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:It's easy to do such a thing. Most programmers do that on a daily or weekly basis, I would guess. I do it regularly. Such events are called "bugs" and it's a normal part of the programmer's job to find and fix them.


This is a useful note about how bugs creep into code. However, while bugs in code are evidently there, which helps with their detection and removal, I have no recollection that I actually made any error(s) while using the option/flag; this means that, unlike with debugging, I have no way of determining the existence of any such hypothetical error(s) I made and removing it/them.
 
Nyeng Gyang
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:Repeated for emphasis!


Thanks, once again, for this effort at assisting with my inquiry. However, the emphasized quote you furnished above is truly superfluous to the assistance I requested.
 
Nyeng Gyang
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My bad. I did not realize that you were responding to the question in title of this thread, rather than to the text of the request for assistance I posted as the first post in the thread.

Matthew Bendford wrote:In addition to that you may recall to may have used -cp in the past but still got issues with it. Which leads to two possible options: Either pointing to the wrong directoy and missing the required package structure or by pointing to individual files rather than to the directories they're in. Or in short: Still getting errors when using -cp looks very strong you used it wrong. By the way you phrased your question and the title of this topic I highly suspect the latter one: Pointing to individual files rather than the directories.


I do not recall doing anything differently that resulted in my now getting the correct behavior of using this option/flag. A problem solution is helped when there is the knowledge and/or existence of the problem. I my case, I have neither the knowledge of the problem that caused the error I was getting nor the existence of this problem. Sorry, but theorizing about possible causes of the error really doesn't help me; trust me when I say this, because I truly do not recall doing anything differently that resulted in my now getting the correct behavior of using this option/flag.
 
Paul Clapham
Marshal
Posts: 26603
81
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nyeng Gyang wrote:I have no recollection that I actually made any error(s) while using the option/flag; this means that, unlike with debugging, I have no way of determining the existence of any such hypothetical error(s) I made and removing it/them.



If you made an error, exactly how and when would you recollect that you made that error? You wouldn't. At some later time you would notice that your use of the feature wasn't working the way you expected, and then you would conclude that you made an error.

And yes, when you make an error in typing something at the command line, it doesn't take long before there's no evidence of what you typed and therefore you do indeed have no way of examining what you typed. And likewise you can't remove the error. All you can do is to realize that what you typed included an error; but as you say it isn't possible to correct the error because you don't know what it was. All you can do is to try again to type a correct command.
 
Nyeng Gyang
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:If you made an error, exactly how and when would you recollect that you made that error? You wouldn't.


There are many errors that I made before, while executing commands at a CLI, which I recall making. Consequently, I think that, while your opinion (in the foregoing quote above) that one wouldn't recall such errors they make is partially accurate, it is not entirely accurate.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic