自定义Java包不起作用 [英] custom java package doesn't work

查看:151
本文介绍了自定义Java包不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个自定义包来放置一些类,但是当我尝试将其导入到我的一个程序中时,它说找不到.

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

对于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.

这篇关于自定义Java包不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆