简体   繁体   中英

Compiling java program with cp switch - unexpected behavior

I'm not able to understand certain behavior while using -cp switch with javac. I have two java files in the directory C:\\A\\B\\C> of a Windows 7 machine. The files are Extend.java and TestExtend.java; both belong to the package 'package com.gonni.profile'. I'm getting the following error:

C:\A\B>javac -d . -cp C\Extend.java
javac: no source files
Usage: javac <options> <source files>
use -help for a list of possible options

C:\A\B>javac -d . -cp 39#$%$fe#%#$%FF#$%GWE C\Extend.java

C:\A\B>javac -d . -cp  C\TestExtend.java
javac: no source files
Usage: javac <options> <source files>
use -help for a list of possible options

C:\A\B>javac -d . -cp 3458$^$%$%BF#W%V#$ C\TestExtend.java
C\TestExtend.java:6: cannot find symbol
symbol  : class Extend
location: class com.gonni.profile.TestExtend
    Extend exObj = new Extend();
    ^
C\TestExtend.java:6: cannot find symbol
symbol  : class Extend
location: class com.gonni.profile.TestExtend
    Extend exObj = new Extend();
                       ^
2 errors

C:\A\B>javac -d . -cp . C\TestExtend.java

C:\A\B>

Extend.java is :

package com.gonni.profile;

class Extend {
    class Inner {

    }
}

TestExtend.java is :

package com.gonni.profile;

class TestExtend {
    public static void main(String[] args) {
        Extend exObj = new Extend();
    }
}

I am sorry to say it but I do not understand what do you want to do: to compile your program or to make javac to fail?

  1. Path C\\TestExtend.java seems wrong. Do you probably mean C:\\TestExtend.java ?
  2. What is 39#$%$fe#%#$%FF#$%GWE ? Do you understand what does -cp mean?
  3. Your classes belong to package com.gonni.profile . It means that they must be under directory com/gonni/profile starting from your source root.
  4. You do not have to supply option -d . . This is a default.
  5. As far as I understand you have several (2 ?) classes without any external dependencies. This means that you do not have to use -cp (that means CLASSPATH) at all.

What to do?

  1. Create directory where your project is. Let's say C:\\myproj .
  2. To simplify things for the beginning create directory structure according to your packages. For exampplee if your package is com.gonni.profile you should create directory C:\\myproj\\com\\gonni\\profile .
  3. Put your class(es) there.
  4. Open commend prompt and go to C:\\proj
  5. Now run command javac com/gonni/profile/*.java

Good luck.

Your source files in this case should be under directory C:\\A\\B\\C\\com\\gonni\\profile - not directy in C:\\A\\B\\C. Option -cp specifies path(s) to look up other compiled classes - not the source files.

Use -sourcepath instead if you want to specify location of source tree:

    javac -sourcepath C C/com/gonni/profile/TestExtend.java

Javac requires to list ALL files it must compile in the command line. You cannot just list one and except it to autodiscover others. As a result, large, real world projects are very difficult to build that way. Also, fix a couple of small errors others have already pointed out.

Hence learn Eclipse, NetBeans or the like for IDE-based development or learn Maven, Ant, Make or the like if you want to become a command line master. It is uncommon just to call javac directly at these times.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM