且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

自定义Java包不起作用

更新时间:2022-06-18 22:22:05

确保没有设置CLASSPATH环境变量.

Make sure you do not have the CLASSPATH environment variable set.

从程序包层次结构的基本目录中编译并运行代码.

Compile and run your code from the base directory of the package hierarchy.

C:\My Documents\Project Euler> javac project_euler\Fibonacci\*.java

C:\My Documents\Project Euler> java project_euler.Fibonacci.test

对于javacjava命令,还可以使用-cp选项显式指定类路径.确保包含程序包层次结构(C:\My Documents\Project Euler)的基本目录.您可以通过在C:\My Documents\Project Euler中指定.(当前目录)来完成此操作:

You can also explicitly specify the classpath using the -cp option for the javac and java commands. Make sure that the base directory of the package hierarchy (C:\My Documents\Project Euler) is included. You could do this by specifying . (the current directory) when you're in C:\My Documents\Project Euler:

C:\My Documents\Project Euler> javac -cp . project_euler\Fibonacci\*.java

C:\My Documents\Project Euler> java -cp . project_euler.Fibonacci.test

注意:根据常见的Java命名约定,您不应在名称(程序包,类,方法名)中使用下划线,程序包名称应为小写字母,并且类名应以大写字母开头.将包重命名为projecteuler.fibonacci(当然,您也需要重命名文件夹),并将类test重命名为Test.

Note: According to the common Java naming conventions, you shouldn't use underscores in names (package, class, method names), package names should be lower-case and class names should start with a capital letter. Rename the package to projecteuler.fibonacci (you'll need to rename the folders too, ofcourse), and rename the class test to Test.