Home java Help to understand why javac -cp not compile class?

Help to understand why javac -cp not compile class?




I have a java – Main2 class on the path C: \ learn \ db \ base \ mine

The class itself looks like

package base.mine;
public class Main2 {
   public static void main (String [] args) {
    List & lt; Animal & gt; a = new ArrayList & lt; & gt; ();
    a.add (new Animal ( "kingoroo", true, false, true));
    a.add (new Animal ( "snake", false, true, false));
    a.add (new Animal ( "fox", true, false, true));
    print (a, new HopeChecker ());
    print ();
    print (a, new HairsChecker ());
    print ();
    print (a, new SwimChecker ());
  private static void print () {
    System.out.println ();
  private static void print (List & lt; Animal & gt; a, Checker checker) {
    for (Animal item: a) {
      if (checker.test (a)) {
        System.out.println (item.getName ());
/ * Kingoroo fox * /
/ * Kingoroo fox * /
/ * Snake * /

If you try to build the project from the project root using the command line – get the error

That is, from


C: \ learn \ db & gt;

run as

C: \ learn \ db & gt; javac -cp "C: \ learn \ db \ base \ mine" Main2. java

and see this error

C: \ learn \ db & gt; javac -cp "C: \ learn \ db \ base \ mine" Main2. java
javac: file not found: Main2.java
Usage: javac & lt; options & gt; & Lt; source files & gt;
use -help for a list of possible options

Why does not compile?

Answer 1, Authority 100%

File Compilation:

When compiling from source class (via console utility javac ) must specify the path to it (relative or absolute).

Your example of using the absolute path:

javac -d target C: /learn/db/base/mine/HelloWorld.java

Your example of using a relative path (relative to the directory that contains your package with the name base ):

javac -d target ./base/mine/HelloWorld.java

In the examples above, the compiled files (.class -files) will be formed in a directory named target (-d target ), it is recommended in order to avoid mixing of source code and assembly results (bytecode).

What did you do wrong: should pay attention to what you are trying to use the parameter -cp (aka -classpath ) at compile time, but this option is only used to indicate dependencies (artifacts containing not compiled your classes that you use in their own code, an example of such an artifact can be .jar -file or directory containing the .class -files, including attachments according to their package ‘s).

Run the compiled file:

To run the compiled file (via the command line utility java ), specify the path to the directory where the assembly is made (in this case, -classpath ./target , or -cp ./target):

java -classpath ./target base.mine.HelloWorld

What to read:

For a more complete understanding of what is happening, you should check out the basic manuals for work with the command line utility in Java, is likely to suit you here this article .

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions