从 Python 运行 Java 程序
虽然信息丰富,但它们还不足以让我发现问题的根源。我的代码不包含在 JAR 文件中,并且客户要求我们不要将其作为此类文件提供。
我在 Eclipse 中构建了该应用程序,并且从那里它运行良好。我已经设置了一个脚本,该脚本将修改其中一个 java 文件的 main
方法以用于测试目的。我想在修改应用程序后使用来自 Python 的调用来运行该应用程序。但是,当我尝试使用 java
或 java -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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
.class
文件所在的目录)调用java -cp .
所在的目录,或者当您创建包时,com
(org
等)子目录所在的目录。com.package.ClassName
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 thecom
(org
, etc) subdirectory resides.com.package.ClassName
您没有提到您的 python 脚本在应用编辑后实际上编译了 java 源文件。
Eclipse 将自动编译它(内置构建器),但当然,在 IDE 之外,您必须先调用 javac,然后才能执行该类。
java
不适用于源文件。编辑
我不知道您对 java 的实际经验 - 但这就是它通常在命令行上工作的方式。
假设你有一个 java 类
com.example.Hello
,它有一个 main 方法和一个文件结构,就像你
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 likethen you
cd
to/dev/bin
and do ajava 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 findHello.class
in the folder./com/example
, then it will throw theNoClassDefFoundError
.简单的解决方法、提示和快捷方式。
从 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")