Java导入的包不存在
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16504603/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java imported package does not exist
提问by user1952529
I am trying to use pdfbox to write a simple pdf file but the problem is that I am getting error :
我正在尝试使用 pdfbox 编写一个简单的 pdf 文件,但问题是出现错误:
cannot find symbol class PDDocument
I have downloaded the jar files into the same folder the program exists. How to fix this compilation error?
我已将 jar 文件下载到程序存在的同一文件夹中。如何修复这个编译错误?
package org.apache.pdfbox.pdmodel.PDDocument;
import java.io.*;
import org.apache.pdfbox.pdmodel.PDDocument;
public class pdf
{
public static void main(String args[])
{
}
}
回答by user1952529
You will need to make sure that the JAR file is on the classpath.
您需要确保 JAR 文件位于类路径中。
回答by Juned Ahsan
Putting the jar in the same folder or package does not add it to the class path. You need to mention the path of the jar in your class path while running your java program. Here is the syntax for that:
将 jar 放在同一文件夹或包中不会将其添加到类路径中。运行java程序时,您需要在类路径中提及jar的路径。这是语法:
To compile:
编译:
javac -classpath .;yourjar.jar src/your/package/*.java
To run
跑步
java -classpath .;yourjar.jar src/your/package/yourprogrammeclassname
回答by Terry
having a similar issue I found that I did not have the correct syntax on the import line in the java source
有一个类似的问题,我发现我在 java 源代码的导入行上没有正确的语法
doing a compile as the following (on windows):
编译如下(在 Windows 上):
javac -cp .;commons-io-2.4.jar AgeFileFilterTest.java
with commons-io-2.4.jar in the same folder as AgeFileFilterTest.java
在与 AgeFileFilterTest.java 相同的文件夹中使用 commons-io-2.4.jar
I was getting error:
我收到错误:
import org.apache.*;
^
AgeFileFilterTest.java:24: error: cannot find symbol
displayFiles(directory, new AgeFileFilter(cutoffDate));
^
It was puzzling becuase it seemed all was in place; the jar was in the folder, defined in the classpath, and upon jar content inspection I could see what was being referenced- using 7zip I opened the jar file and could see:
这令人费解,因为一切似乎都已就位;jar 位于文件夹中,在类路径中定义,在检查 jar 内容时,我可以看到所引用的内容 - 使用 7zip 我打开了 jar 文件,可以看到:
commons-io-2.4.jar\org\apache\commons\io\filefilter\AbstractFileFilter.class
I then read in some post "you do not import the class"which got me to think about the import syntax...
然后我在一些帖子中读到“你不导入类”这让我想到了导入语法......
I changed it from:
我把它从:
import org.apache.*;
changing it to:
将其更改为:
import org.apache.commons.io.filefilter.*;
and wala compile error gone using: javac -cp .;commons-io-2.4.jar AgeFileFilterTest.java
和 wala 编译错误消失了使用:javac -cp .;commons-io-2.4.jar AgeFileFilterTest.java
and program worked using
和程序使用
java -cp .;commons-io-2.4.jar AgeFileFilterTest