【发布时间】:2013-10-30 23:40:39
【问题描述】:
我查看了该网站上的其他几个答案,试图了解为什么会发生这种情况,但我不明白我做错了什么。
我正在尝试开始使用 iText 和一般的 .jar 文件。我将 iText .jar 文件下载并解压缩到桌面上的一个文件夹中:Desktop\Java\itext-5.4.4\"jar files here"
然后我去了以下网站:
http://tutorials.jenkov.com/java-itext/getting-started.html
并将代码复制到记事本中。它看起来像这样:
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
/**
*/
public class HelloWorldExample {
public static void main(String[] args) {
Document document = new Document();
try {
PdfWriter.getInstance(document,
new FileOutputStream("HelloWorld.pdf"));
document.open();
document.add(new Paragraph("A Hello World PDF
document."));
document.close(); // no need to close
PDFwriter?
} catch (DocumentException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
我在桌面上将文件保存为 HelloWorldExample.java
然后我用以下命令编译我的代码:
cd c:\桌面
javac -classpath Java\itext-5.4.4\itextpdf-5.4.4.jar HelloWorldExample.java
编译成功
然后我尝试了:
java -classpath Java\itext-5.4.4\itextpdf-5.4.4.jar HelloWorldExample
我得到错误:无法找到或加载主类 HelloWorldExample 错误。
我已经尝试了很多变体,包括创建一个文件夹、在该文件夹中放置一个 lib 文件夹以及创建一个包,但仍然得到相同的错误。
这里发生了什么?
谢谢!
【问题讨论】:
-
你确定 HelloWorldExample.class 在 java 命令的 -classpath 选项指定的类路径中吗?
-
@crybird 我的 HelloWorldExample.class 不在 classpath 命令指定的目录中 - 它被编译到与 .java 文件相同的目录,即桌面。我尝试选择 .class 文件并将其移动到该文件夹 - 同样的错误。我只是使用this 建议的命令,这似乎暗示了这一点——我不知道是否有一些系统变量导致问题或什么。
-
您指定的类路径必须包含您编译的类。