自定义Java包不起作用

编程入门 行业动态 更新时间:2024-10-10 19:26:39
本文介绍了自定义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

对于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包不起作用

本文发布于:2023-11-02 06:02:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1551565.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   不起作用   Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!