从 Python 运行 Java 程序

发布于 2024-08-19 09:15:53 字数 1772 浏览 2 评论 0原文

我看过这里此处此处,以及此处

虽然信息丰富,但它们还不足以让我发现问题的根源。我的代码不包含在 JAR 文件中,并且客户要求我们不要将其作为此类文件提供。

我在 Eclipse 中构建了该应用程序,并且从那里它运行良好。我已经设置了一个脚本,该脚本将修改其中一个 java 文件的 main 方法以用于测试目的。我想在修改应用程序后使用来自 Python 的调用来运行该应用程序。但是,当我尝试使用 javajava -cp . 从命令行调用程序时,出现以下错误:

Exception in thread "main" java.lang.NoClassDefFoundError: DemoAPIFunctionality
Caused by: java.lang.ClassNotFoundException: DemoAPIFunctionality
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClassInternal(Unknown Source)
Could not find the main class: DemoAPIFunctionality.  Program will exit.

我的项目依赖于多个 JAR,但这些 JAR 不是在根目录中,还有一个DLL文件,同样不在根目录中。我希望它与其中之一的位置有关,但我不确定并且进展甚微。这里是 DemoAPIFunctionality.java:

public class DemoAPIFunctionality
{
  public final static void main(String[] args)
  {
    DemoAPIFunctionality demo = new DemoAPIFunctionality();

如果能帮助您,我很乐意列出更多信息。 (不,我不是体育经纪人。)我需要做什么才能让它至少从命令行工作?

提前致谢。

I've looked here, here, here, and here.

While informative they just didn't quite have enough for me to discover the root of my problem. My code isn't contained within a JAR file and the customer has requested we do not ship it as such.

I built the application in Eclipse and from there it runs fine. I've set up a script that will modify the main method of one of the java files for testing purposes. I want to run the application using a call from Python after I modify it. However, from the command line when I attempt to call the program using java or java -cp . I get the following error:

Exception in thread "main" java.lang.NoClassDefFoundError: DemoAPIFunctionality
Caused by: java.lang.ClassNotFoundException: DemoAPIFunctionality
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClassInternal(Unknown Source)
Could not find the main class: DemoAPIFunctionality.  Program will exit.

My project depends on several JARs, which are not within the root directory, and a DLL file, again not in the root directory. I'm hoping it's related to the location of one of those but I'm not sure and am making little headway. Here is DemoAPIFunctionality.java:

public class DemoAPIFunctionality
{
  public final static void main(String[] args)
  {
    DemoAPIFunctionality demo = new DemoAPIFunctionality();

I'd happily list any more information if it will help you help me. (No, I'm not a sports agent.) What do I need to do to get it to work at least from the command line?

Thanks in advance.

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

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

发布评论

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

评论(3

套路撩心 2024-08-26 09:15:53
  1. 不鼓励在默认包(即没有包)中包含类,
  2. 您必须从项目的根目录(即 .class 文件所在的目录)调用 java -cp .所在的目录,或者当您创建包时,comorg 等)子目录所在的目录。
  3. 您必须通过完整的限定名称来引用该类 - 即 com.package.ClassName
  4. 您最好使用 IDE - 出于教育目的,有 BlueJ 和 JCreator。 Eclipse 和 NetBeans 更强大、更复杂。
  1. having classes in the default package (i.e. no package) is discouraged
  2. you must call java -cp . from the root directory of your project - i.e. the directory where your .class file resides, or when you create packages, the directory, where the com (org, etc) subdirectory resides.
  3. you must refer to the class by it full qualified name - i.e. com.package.ClassName
  4. you'd better use an IDE - for educational purposes there's BlueJ and JCreator. Eclipse and NetBeans are more powerful and complex.
我是男神闪亮亮 2024-08-26 09:15:53

您没有提到您的 python 脚本在应用编辑后实际上编译了 java 源文件。

Eclipse 将自动编译它(内置构建器),但当然,在 IDE 之外,您必须先调用 javac,然后才能执行该类。 java 不适用于源文件。

编辑

我不知道您对 java 的实际经验 - 但这就是它通常在命令行上工作的方式。

假设你有一个 java 类 com.example.Hello ,它有一个 main 方法和一个文件结构,就像

/dev/bin/com/example/Hello.class

cd/dev/bin并执行java com.example.Hello。请注意,包名称 (com.example) 始终映射到文件夹结构 (com/example)。如果执行上述命令,java 在 ./com/example 文件夹中找不到 Hello.class,那么它会抛出 NoClassDefFoundError >。

You didn't mention that your python script actually compiles the java source file after applying the edits.

Eclipse will compile it automatically (build-in builder) but of course, outside of the IDE you have to call javac before you can execute the class. java will not work on source files.

EDIT

I don't know you actual experience in java - but that's how it usually works on the command line.

Assuming, you have a java class com.example.Hello with a main method and a file structure like

/dev/bin/com/example/Hello.class

then you cd to /dev/bin and do a java com.example.Hello. Note that the package name (com.example) is always mapped to a folder structure (com/example). If you do the above command and java can't find Hello.class in the folder ./com/example, then it will throw the NoClassDefFoundError.

不知所踪 2024-08-26 09:15:53

简单的解决方法、提示和快捷方式。
从 python 中调用 java classname 时不要使用任何路径。
例如。 commands.getstatusoutput("java -cp $CLASSPATH clusterize")

Easy workaround, hint and shortcut.
Dont use any path when calling java classname from within python.
eg. commands.getstatusoutput("java -cp $CLASSPATH clusterize")

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文