如何使用 eclipse IDE 运行 API 示例(已经有工作区)

发布于 2024-10-13 10:24:46 字数 2661 浏览 3 评论 0原文

我是 Java 新手,我不确定我的问题标题是否恰当。请推荐更好的标题。

我有一个 Java API,它有许多可执行模块,可以执行与解析 MS Outlook 的 .pst 文件相关的各种操作。我的问题是,当我运行 execute 命令来执行 API 一个模块的 .class 文件时,出现此异常(java -classpath / opt/Java/libs/JPST/lib/jpst.jar /opt/Java/libs/JPST/examples/GetInboxMessages/bin/Example)-

Exception in thread "main" java.lang.NoClassDefFoundError: /opt/Java/libs/JPST/examples/GetInboxMessages/bin/Example
Caused by: java.lang.ClassNotFoundException: .opt.Java.libs.JPST.examples.GetInboxMessages.bin.Example

        at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: /opt/Java/libs/JPST/examples/GetInboxMessages/bin/Example.  Program will exit.

到目前为止...
您可以检查 我之前的问题,我得出的结论是我正确运行了compileexecute命令。但我仍然遇到上述“例外”。

我询问 API 开发人员,他说我应该使用 Eclipse 或 Netbeans IDE。他说

API 内的

examples 文件夹 文件夹结构是整个Eclipse 工作区。

还说

编译和运行更容易 示例。

现在...
因此,我已经为 Java 设置了 Eclipse Helios。谁能告诉我如何使用 IDE 轻松编译和运行所有示例(我认为是示例文件夹中的模块)。我之前曾使用过 Eclipse 来处理 PHP,但从未使用过 Java。

API的文件夹结构
我需要执行这些模块之一的 .class 文件(示例目录中存在许多模块文件夹)。 API 的示例文件夹中存在许多此类模块。每个模块文件夹都包含一个 bin 文件夹(包含 .class 文件)和 src 文件夹(包含 .java文件) 。我需要执行一个这样的 .class 文件。

有一个 lib 目录,其中包含一个 .jar 文件(我的目标 .class 文件需要引用该文件)。 lib 目录与 examples 目录处于同一级别。

解决方案
我已经验证了 Jonathan 给出的解决方案是否有效(仅验证了不使用省略号的情况)。为了更清楚地说明,classpath 需要包含编译和执行命令中的所有类位置。因此,如果您需要编译并执行具有依赖项 Parent.jarExample.java 文件,请执行以下操作:-

编译

javac -classpath /path/to/Parent.jar/file/Parent.jar:/path/to/Example.java/file/ Example.java

执行

java -classpath /path/to/Parent.jar/file/Parent.jar:/path/to/Example.class/file Example

并注意分隔符 - 应为 : 适用于 Linux,; 适用于 Windows。

谢谢,
桑迪潘

I am new to Java and I am not sure how apt is my question title. Please suggest any better title.

I have got a Java API which has many executable modules which do various things related to parsing MS Outlook's .pst files. And my problem is that I am getting this exception when I run the execute command to execute a .class file of one module of the API(java -classpath /opt/Java/libs/JPST/lib/jpst.jar /opt/Java/libs/JPST/examples/GetInboxMessages/bin/Example)-

Exception in thread "main" java.lang.NoClassDefFoundError: /opt/Java/libs/JPST/examples/GetInboxMessages/bin/Example
Caused by: java.lang.ClassNotFoundException: .opt.Java.libs.JPST.examples.GetInboxMessages.bin.Example

        at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: /opt/Java/libs/JPST/examples/GetInboxMessages/bin/Example.  Program will exit.

So far ...
You can check my previous question where I concluded that I was correctly running the compile and execute commands. But I am still getting the above mentioned `exception.

I asked the API's developer who says that I should get Eclipse or Netbeans IDE. He said that the

examples folder inside the API's
folder structure is entire Eclipse
workspace.

and also said that

It is easier to compile and run all
examples.

And now ...
So, I have setup Eclipse Helios for Java. Can anyone tell me how is it easy to compile and run all the examples (I think the modules inside the examples folder) using the IDE. I have earlier worked with Eclipse for PHP but never for Java.

Folder structure of the API
I need to execute a .class file of one of those modules (many module folders are present inside examples directory). There are many such modules present inside the examples folder of the API. Each of these modules folders contain a bin folder (containing .class file) and src folder (containing .java file) . I need to execute one such .class file.

There is a lib directory containing a .jar file (which is needed to be referenced by my target .class file). The lib directory is at the same level as the examples directory.

Solution
I have verified that the solution given by Jonathan works (verified only without-using-elipse case). To make it more clear the classpath needs to include all the class locations in both compile and execute commands. So, if you need to compile and execute an Example.java file with a dependency Parent.jar do this:-

Compile

javac -classpath /path/to/Parent.jar/file/Parent.jar:/path/to/Example.java/file/ Example.java

Execute

java -classpath /path/to/Parent.jar/file/Parent.jar:/path/to/Example.class/file Example

And note the separator - should be : for linux and ; for windows.

Thanks,
Sandeepan

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅沫记忆 2024-10-20 10:24:46

当 Eclipse 启动时,它应该询问您要使用哪个工作区。选择示例文件夹,如果它确实是 Eclipse 工作区,您应该会看到已设置的所有项目(可能需要一些时间来构建所有内容,具体取决于项目的大小和计算机的速度)。然后只需找到您想要的类并运行它即可。如果一切设置正确,它应该可以正常工作。如果没有,您可能需要返回开发商以获取更多详细信息。

但我认为您不需要 Eclipse 来运行该类。查看您在问题开头提供的堆栈跟踪,它似乎无法运行,因为您没有正确设置类路径。尝试:

java -classpath /opt/Java/libs/JPST/lib/jpst.jar:/opt/Java/libs/JPST/examples/GetInboxMessages/bin Example

非常简单地解释一下:类路径列出了 Java 查找所需类的位置。您指定要相对于类路径运行的类,而不是作为文件路径。由于您尝试在文件夹“/opt/Java/libs/JPST/examples/GetInboxMessages/bin”中运行的类,因此该文件夹必须位于类路径上(注意,我假设基于 UNIX 的系统,如果您使用的是Windows 使用“;”来分隔条目,而不是“:”)。然后简单地指定要相对于类路径自行运行的类(在本例中只是类名本身)。

请注意,这是一个非常基本的概述,如果您开始使用包(出现在 bin 文件夹下方的文件夹),事情会变得更加复杂。这是使用 Eclipse 这样的程序的优点之一,因为它会为您处理很多事情。

When Eclipse starts it should ask you which workspace to use. Select the examples folder and, if it really is an eclipse workspace, you should see all the projects already set up (it may take some time to build everything depending on the size of the projects and the speed of your machine). Then simply find the class you want and run it. If everything is set up correctly it should just work. If not you may have to go back to the developer for more details.

But I don't think you need Eclipse to run that one class. Looking at the stacktrace you provided at the beginning of your question it looks like it failed to run because you haven't set your classpath properly. Try:

java -classpath /opt/Java/libs/JPST/lib/jpst.jar:/opt/Java/libs/JPST/examples/GetInboxMessages/bin Example

To very briefly explain: The classpath lists the places where Java looks for the classes it needs. You specify the class to run relative to the classpath, not as a file path. Since the class you are trying to run in in the folder "/opt/Java/libs/JPST/examples/GetInboxMessages/bin" this folder must be on the classpath (note, I am assuming a unix based system, if you are using windows use ';' to separate entries not ':'). Then simply specify the class to run by itself relative to the classpath (which in this case is just the class name by itself).

Please be aware this is a very basic overview, things get a more complex if you start using packages (folders that would appear below the bin folder). This is one advantage of using a program like Eclipse since it will handle a lot of this for you.

一笑百媚生 2024-10-20 10:24:46

听起来像是类路径问题。

  • 右键单击 Eclipse 中的项目。

  • 转到构建路径>配置构建路径

  • 确保在库选项卡中引用了 jar 文件,如果没有则添加它。

Sounds like a classpath issue.

  • Right click on your project in eclipse.

  • Go to buildPath > configure build path

  • Make sure the jar file is referenced in the libraries tab and if it isn't add it.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文