Jython 不会导入用户定义的类;导入错误:没有名为 ****** 的模块
这几天我一直在用头撞墙,试图解决这个问题。我已经开始使用 Jython 进行快速原型设计。我遇到了一个看似非常基本的问题,但我似乎无法克服它。
我在 JythonBook 第 10 章 中,当我遇到问题时尝试编写并使用“Beach”类(从标记为“清单10-1”的部分开始)。我可以很好地导入和使用 java.lang.Math,但无法让“Beach”类为我的生活工作。我编写了 Beach 类,将其变成 jar 并更改了权限,
jar cf Beach.jar Beach.java chmod 777 海滩.jar
并确保 Beach.jar 和 Beach.java 都在当前工作目录中,以及 /Library/Java/Extensions/ (我在 Mac 上)——没有骰子。
我只是想不出可能是什么问题。我希望这里有人能看到我错过的东西。
为了您的方便,我认为可能有用的所有内容都来自“实时”Jython 会话:
$ jython
Jython 2.5.2 (Release_2_5_2:7206, Mar 2 2011, 23:12:06)
[Java HotSpot(TM) 64-Bit Server VM (Apple Inc.)] on java1.6.0_24
Type "help", "copyright", "credits" or "license" for more information.
>>> ## First try importing "Math" from Java:
>>> from java.lang import Math
>>> Math.max(4, 7)
7L
>>> ## Try System from Java:
>>> javasystem.out.println("Hello")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'javasystem' is not defined
>>> from java.lang import System as javasystem
>>> javasystem.out.println("Hello")
Hello
>>> ##
>>> ## Now try to import my own class, written as in the JythonBook
>>> ## This is where the trouble starts:
>>> ## First check that I am in the correct place, look at the system path:
>>> import os
>>> os.system("pwd")
/Users/me/EclipseProjects/JythonTutorial/JavaClasses
0
>>> os.system("ls -la")
total 16
drwxr-xr-x 4 me staff 136 Oct 19 11:25 .
drwxr-xr-x 7 me staff 238 Oct 19 02:16 ..
-rwxrwxrwx 1 me staff 567 Oct 19 11:25 Beach.jar
-rwxrwxrwx 1 me staff 256 Oct 19 11:14 Beach.java
0
>>> ## Let's look at the guts of Beach.java quickly:
>>> os.system("cat Beach.java")
public class Beach {
private String name;
public Beach(String name, String city){
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
0
>>> ## Let's also look at the system path:
>>> import sys
>>> sys.path
['', '/Users/me/jython2.5.2/Lib', '__classpath__', '__pyclasspath__/', '/Users/me/jython2.5.2/Lib/site-packages']
>>> ## I presume that the '' should indicate that I can use this.
>>> ## To be careful, I also cp Beach.jar to /Library/Java/Extensions/
>>> os.system("ls -la /Library/Java/Extensions/Beach.jar")
-rwxr-xr-x 1 me admin 567 Oct 19 11:27 /Library/Java/Extensions/Beach.jar
0
>>> ##
>>> ## Now actually attempt to load Beach:
>>> import Beach
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named Beach
>>> from Beach import Beach
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named Beach
>>>
我还应该发布我的 ~/.bash_profile 吗?
我在互联网和 StackOverflow 上闲逛了很多。 这篇文章给了我尝试“目标导入”的想法,但作为你可以看到这不起作用。我似乎无法让它发挥作用。在阅读了一些关于混乱 CLASSPATH 的恐惧之后,我不再管它了。
也许我应该指出,我对编程并不陌生——我是一名计算机科学本科生,并且在过去 6 年左右的时间里一直在编码(主要是科学计算)。我对 Java 还很陌生;我今年早些时候刚刚开始使用 Java 编程。这看起来非常简单,NetBeans 已经解决了我不知道的所有问题。
我担心我误解了 Java 的类路径。我还将一些 jar 移动到了我认为应该由 Java “搜索”的位置 - /System/Library/Java/Extensions/、/Library/Java/Extensions/ 和 /usr/lib/java/ (我知道......),但似乎没有什么可以改变上述错误。
(可能相关的说明是,在尝试过此操作后,Netbeans 似乎无法填充新项目 - 我现在正在手动执行此操作。不确定这是否是有用的信息。)
有什么想法吗?我非常感谢任何帮助!
编辑:尝试下面的评论后,我得到以下信息:
$ jython
Jython 2.5.2 (Release_2_5_2:7206, Mar 2 2011, 23:12:06)
[Java HotSpot(TM) 64-Bit Server VM (Apple Inc.)] on java1.6.0_24
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path.append("/Users/me/EclipseProjects/JythonTutorial/JavaClasses/Beach.jar")
>>> from com.stackoverflow.beach import Beach
*sys-package-mgr*: processing modified jar, '/Users/me/EclipseProjects/JythonTutorial/JavaClasses/Beach.jar'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named stackoverflow
>>> ## Look in Beach.java
>>> import os
>>> os.system("cat Beach.java")
// Beach.java
package com.stackoverflow.beach;
public class Beach {
private String name;
public Beach(String name, String city){
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
>>>
我知道Java包是基于文件结构的。如果这里有明显错误的地方,请告诉我。与此同时,是时候去了解 Java 包的工作原理了(而不是让 Netbeans 总是管理它)。
I've been banging my head against the wall for a couple days now trying to figure this out. I've been starting to play around with Jython for fast prototyping. I've hit what appears to be an incredibly basic problem but I just can't seem to get past it.
I was in Ch 10 of the JythonBook and hit the problem when I tried to write and use the "Beach" class (starting in the section labeled "Listing 10-1"). I could import and use java.lang.Math just fine, but couldn't get the "Beach" class to work for the life of me. I wrote the Beach class, turned it into a jar and changed permissions,
jar cf Beach.jar Beach.java
chmod 777 Beach.jar
and made sure that both Beach.jar and Beach.java were in the current working directory, as well as /Library/Java/Extensions/ (I'm on a Mac) -- no dice.
I just can't think what might be the problem. I'm hoping someone here will see something I missed.
For your convenience, everything that I think may be useful, from a "live" Jython session:
$ jython
Jython 2.5.2 (Release_2_5_2:7206, Mar 2 2011, 23:12:06)
[Java HotSpot(TM) 64-Bit Server VM (Apple Inc.)] on java1.6.0_24
Type "help", "copyright", "credits" or "license" for more information.
>>> ## First try importing "Math" from Java:
>>> from java.lang import Math
>>> Math.max(4, 7)
7L
>>> ## Try System from Java:
>>> javasystem.out.println("Hello")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'javasystem' is not defined
>>> from java.lang import System as javasystem
>>> javasystem.out.println("Hello")
Hello
>>> ##
>>> ## Now try to import my own class, written as in the JythonBook
>>> ## This is where the trouble starts:
>>> ## First check that I am in the correct place, look at the system path:
>>> import os
>>> os.system("pwd")
/Users/me/EclipseProjects/JythonTutorial/JavaClasses
0
>>> os.system("ls -la")
total 16
drwxr-xr-x 4 me staff 136 Oct 19 11:25 .
drwxr-xr-x 7 me staff 238 Oct 19 02:16 ..
-rwxrwxrwx 1 me staff 567 Oct 19 11:25 Beach.jar
-rwxrwxrwx 1 me staff 256 Oct 19 11:14 Beach.java
0
>>> ## Let's look at the guts of Beach.java quickly:
>>> os.system("cat Beach.java")
public class Beach {
private String name;
public Beach(String name, String city){
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
0
>>> ## Let's also look at the system path:
>>> import sys
>>> sys.path
['', '/Users/me/jython2.5.2/Lib', '__classpath__', '__pyclasspath__/', '/Users/me/jython2.5.2/Lib/site-packages']
>>> ## I presume that the '' should indicate that I can use this.
>>> ## To be careful, I also cp Beach.jar to /Library/Java/Extensions/
>>> os.system("ls -la /Library/Java/Extensions/Beach.jar")
-rwxr-xr-x 1 me admin 567 Oct 19 11:27 /Library/Java/Extensions/Beach.jar
0
>>> ##
>>> ## Now actually attempt to load Beach:
>>> import Beach
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named Beach
>>> from Beach import Beach
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named Beach
>>>
Should I also post my ~/.bash_profile ?
I've poked around the internet and StackOverflow quite a bit. This post gave me the idea to try to "target import," but as you can see this didn't work. I just can't seem to get this to work. After reading a bit about terrors of messing eith CLASSPATH, I've left that alone.
I should perhaps note that I'm not new to programming -- I was a CS undergrad and have coded (mostly scientific computing) for the last 6 or so years. I am fairly new to Java; I just started programming in Java earlier this year. It seems pretty straightforward, and NetBeans has taken care of everything I don't know.
My fear is that I'm misunderstanding something about the classpath for Java. I've also moved some jars around to the the locations that I think should be "searched" by Java -- /System/Library/Java/Extensions/, /Library/Java/Extensions/, and /usr/lib/java/ (I know...), but nothing seems to change the above errors.
(On a possibly related note, after playing around with this, Netbeans appears to not be able to populate a new project -- I'm doing that manually now. Not sure if that is helpful info.)
Any thoughts? I'm incredibly grateful for any help!
EDIT: After trying out the comment below, I got the following:
$ jython
Jython 2.5.2 (Release_2_5_2:7206, Mar 2 2011, 23:12:06)
[Java HotSpot(TM) 64-Bit Server VM (Apple Inc.)] on java1.6.0_24
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path.append("/Users/me/EclipseProjects/JythonTutorial/JavaClasses/Beach.jar")
>>> from com.stackoverflow.beach import Beach
*sys-package-mgr*: processing modified jar, '/Users/me/EclipseProjects/JythonTutorial/JavaClasses/Beach.jar'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named stackoverflow
>>> ## Look in Beach.java
>>> import os
>>> os.system("cat Beach.java")
// Beach.java
package com.stackoverflow.beach;
public class Beach {
private String name;
public Beach(String name, String city){
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
>>>
I know that Java packages are based on file structure. If there is something entirely obviously wrong here, please let me know. In the meanwhile, time to go read up on how Java packages work (instead of letting Netbeans always manage it).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
jython 无法导入 *.java 文件,您需要将其编译为 *.class。
Makefile:
示例
其他文件
test_beach.py:
Beach.java:
jython can't import *.java file you need to compile it to *.class.
Makefile:
Example
Other files
test_beach.py:
Beach.java:
我认为这个例子可能有错误。当您在 Jython 中
导入 X
时,它会查找名为X
的 Java包,而不是名为 类代码>X。尝试:
在 Jython 中:
编辑: 另外,您可能需要确保
.jar
文件的全名——而不仅仅是它所在的目录—— - 列在CLASSPATH
中。这在 Java 中当然是必要的,我认为 Jython 在这方面的规则是相同的。I think there may be an error in that example. When you
import X
in Jython, this looks for a Java package namedX
, not for a class namedX
.Try:
and in Jython:
edit: Also, you might want to make sure that the full name of the
.jar
file -- and not just the directory where it resides -- is listed on theCLASSPATH
. This is certainly necessary in Java, and I assume Jython's rules are the same in this regard.