简体   繁体   English

使用cp开关编译Java程序-意外行为

[英]Compiling java program with cp switch - unexpected behavior

I'm not able to understand certain behavior while using -cp switch with javac. 使用javac的-cp开关时,我无法理解某些行为。 I have two java files in the directory C:\\A\\B\\C> of a Windows 7 machine. 我在Windows 7计算机的目录C:\\ A \\ B \\ C>中有两个Java文件。 The files are Extend.java and TestExtend.java; 文件是Extend.java和TestExtend.java; both belong to the package 'package com.gonni.profile'. 两者都属于软件包“ 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 : Extend.java是:

package com.gonni.profile;

class Extend {
    class Inner {

    }
}

TestExtend.java is : TestExtend.java是:

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? 不好意思地说,但是我不明白您想做什么:编译程序或使javac失败?

  1. Path C\\TestExtend.java seems wrong. 路径C\\TestExtend.java似乎错误。 Do you probably mean C:\\TestExtend.java ? 您可能是指C:\\TestExtend.java吗?
  2. What is 39#$%$fe#%#$%FF#$%GWE ? 什么是39#$%$fe#%#$%FF#$%GWE Do you understand what does -cp mean? 您了解-cp是什么意思吗?
  3. Your classes belong to package com.gonni.profile . 您的课程属于com.gonni.profile包。 It means that they must be under directory com/gonni/profile starting from your source root. 这意味着它们必须从源根开始位于目录com/gonni/profile
  4. You do not have to supply option -d . 您不必提供选项-d . . This is a default. 这是默认值。
  5. As far as I understand you have several (2 ?) classes without any external dependencies. 据我了解,您有几个(2?)类,没有任何外部依赖项。 This means that you do not have to use -cp (that means CLASSPATH) at all. 这意味着您根本不必使用-cp (即CLASSPATH)。

What to do? 该怎么办?

  1. Create directory where your project is. 创建项目所在的目录。 Let's say C:\\myproj . 假设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 . 例如,如果您的软件包是com.gonni.profile ,则应创建目录C:\\myproj\\com\\gonni\\profile
  3. Put your class(es) there. 将您的课程放在那里。
  4. Open commend prompt and go to C:\\proj 打开命令提示符,然后转到C:\\proj
  5. Now run command javac com/gonni/profile/*.java 现在运行命令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. 在这种情况下,您的源文件应位于目录C:\\ A \\ B \\ C \\ com \\ gonni \\ profile下-不直接位于C:\\ A \\ B \\ C中。 Option -cp specifies path(s) to look up other compiled classes - not the source files. 选项-cp指定查找其他编译类的路径-而不是源文件。

Use -sourcepath instead if you want to specify location of source tree: 如果要指定源树的位置,请使用-sourcepath

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

Javac requires to list ALL files it must compile in the command line. Javac要求列出必须在命令行中编译的所有文件。 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. 因此,如果您想成为命令行大师,请学习Eclipse,NetBeans等用于基于IDE的开发,或者学习Maven,Ant,Make等。 It is uncommon just to call javac directly at these times. 仅在这些时间直接调用javac并不常见。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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