• 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
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Himai Minh
  • Carey Brown
  • salvin francis

How to use -cp in javac command?

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

The book gives an example of -cp using the java run command:

java -cp classes packageb.ClassB

It worked. But when I try to compile ClassB using the javac -cp it gives me two errors:

error: Class names, 'packageb/ClassB', are only accepted if annotation processing is explicitly requested

Here's the file structure:
/src/classes/packageb/

Only two files in the directory: ClassB.java, ClassB.class

Can someone please show me how to use the -cp option with the javac command?

Thanks!
 
Marshal
Posts: 71047
291
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which book?

The reason for the failure to start the compiler is that it expects you to write XXX.java after javac.
 
Rancher
Posts: 3968
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

how to use the -cp option with the javac command?


You use the -cp option to add paths to the classpath so that the java or javac command can find them. If the java file that is being compiled by the javac command does not reference any packages (ie with an import statement) then there is no need to use the -cp option to provide a path to the imported package/class.

If there was a packageb.ClassA class in the packageb folder that was included in the ClassB class, then the classpath would need to point to the folder containing the packageb folder.  For example if the javac command is issued in the packageb folder the the class path would need to point to the folder up one level:   javac -cp ..\.;. ClassB.java    (that uses Windows path notations, I don't know the unix version);
 
Saloon Keeper
Posts: 22784
153
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Unix/Linux syntax would be "javac -cp ..:. ClassB.java". Actually, "..\." and ".." are equivalent. Note that the semicolon between ".." and "." becomes a colon, since the semi-colon is used to separate multiple commands on a Unix shell command line. Windows couldn't use ":" since if your class director was named "C", then "cp C:foo" would be misread, if you actually meant "C;foo" (two separate directories).

And -cp is the same as -classpath on the javac command, just shorter.

I should point mention that using ".." (parent directory) and "." (current directory) in a classpath is not something you'd usually do. A more realistic classpath usage might be something like this:


Which means: compile file src/test/java/com/coderanch/myapp/MyTest,java referencing classes in the dist/classes and test/classes directory subtrees and placing the compiled output in the file test/classes/com/coderanch/myapp/MyTest.class.

 
Cosmid Constantine
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the replies everyone! Sorry I don't know how to reply to a specific person. I couldn't find the 'reply' button for each comment, like on most forums.

@Campbell, the name of the book is OCP Oracle Certified Professional Java SE11 Developer Complete Study Guide. I had the .java in the command but forgot to copy over my .java file to that directory.

@Norm, yes, the book was trying to show how to use the -cp option but only provided an example with java and not javac. It first exemplified with a -d option using the javac, so the classes directory didn't include any .java files and that's why I failed with my javac -cp command.

@Tim, yes, the book tried to show the 3 different versions of the -cp options, I honestly think the --class-path is useless and is there only to make people lose points on the exam. Thanks for providing the example, a bit complicated, but helped me understand it better.
 
Tim Holloway
Saloon Keeper
Posts: 22784
153
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason for "--class-path" as an option is to be consistent with long parameter name conventions in Linux/Unix environments. Note that it begins with 2 dashes, not one, which is how long names are indicated to the standard command-line parser. And yes, I wasn't aware of that option myself. It's actually the "-classpath" option that's out of step, but I suspect that perhaps originally Sun used an ad-hoc command-line parser, since back then, SunOS/Solaris was their home platform, and it tended to lag Linux on cool features.

I haven't tried it, but if the command syntax is fully-conformant, an acceptable version of this argument would be something like "--class-path=dist/classes:test/classes". That's because the convention allows both --long-name=value and --long-name value (space after long-name) for the convenience of things like shell scripts where you'd have confusion about whether it was one parameter or two.
 
Cosmid Constantine
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ahh, I see. Thanks for explaining that to me.
 
I've been selected to go to the moon! All thanks to 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