[英]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
失败?
C\\TestExtend.java
seems wrong. C\\TestExtend.java
似乎错误。 Do you probably mean C:\\TestExtend.java
? C:\\TestExtend.java
吗? 39#$%$fe#%#$%FF#$%GWE
? 39#$%$fe#%#$%FF#$%GWE
? Do you understand what does -cp
mean? -cp
是什么意思吗? 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
。 -d .
-d .
. -cp
(that means CLASSPATH) at all. -cp
(即CLASSPATH)。 What to do? 该怎么办?
C:\\myproj
. C:\\myproj
。 com.gonni.profile
you should create directory C:\\myproj\\com\\gonni\\profile
. com.gonni.profile
,则应创建目录C:\\myproj\\com\\gonni\\profile
。 C:\\proj
C:\\proj
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.