我试图创建一个自定义包来放置一些类,但是当我尝试将其导入到我的一个程序中时,它说找不到.
I am trying to create a custom package to put some of my classes, but when I try to import it into one of my programs, it says it cant be found.
这是我要编译的文件,但表示找不到该软件包
This is the file I am trying to compile but it is saying the package cannot be found
import project_euler.Fibonacci; public class test { public static void main(String[] args) { Fibonacci fib = new Fibonacci(); System.out.println(fib.getTerm(10)); } }这是斐波那契课程
package project_euler; public class Fibonacci { public int getTerm(int n) { if (n < 0 || n > 46) { throw new IllegalArgumentException(); } else { return (n > 1) ? getTerm(n-1) + getTerm(n-2) : n; } } }这是我尝试编译时遇到的错误
This is the errors I get when I try to compile
test.java:1: error: package project_euler does not exist import project_euler.Fibonacci; ^ test.java:6: error: cannot access Fibonacci Fibonacci fib = new Fibonacci(); ^ bad source file: C:\Users\dhout_000\Documents\Project Euler\project_euler\Fibonacci.java file does not contain class Fibonacci Please remove or make sure it appears in the correct subdirectory of the sourcepath. 2 errors我的目录设置是
> My Documents > Project Euler - test.java > project_euler - Fibonacci.class - Fibonacci.java我似乎无法弄清楚问题出在哪里
I just cant seem to figure out what the problem is
推荐答案确保没有设置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对于javac和java命令,还可以使用-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.
更多推荐
自定义Java包不起作用
发布评论