Jython,未解决的导入 - java class
Jython, unresolved import - java class
我正在试用 jython,很早就被第 10 章的示例卡住了 http://www.jython.org/jythonbook/en/1.0/JythonAndJavaIntegration.html java 库中的数学导入确实有效。但是我无法让自定义 class 海滩工作。
我看到了一个相同的 post Jython won't import user-defined class; ImportError: No module named ******
但是我不明白接受的答案方法。
所以我希望得到一些澄清。
下面是我的项目结构。
import Beach
b = Beach("Cocoa Beach","Cocoa Beach")
print (b.getName(Beach.getName()))
导入海滩:未解决的导入:海滩
编辑:让它与以下内容一起工作:
import sys
sys.path.append('C:\Users\Rasmus\Desktop')
import Beach
beach = Beach("Cocoa Beach","Cocoa Beach")
beach.getName()
print(beach.getName())
但是 pydev eclipse 仍然将导入海滩标记为未解决的导入。
海滩class
public class Beach {
private String name;
private String city;
public Beach(String name, String city){
this.name = name;
this.city = city;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
}
您需要在 Jython class 路径中包含已编译的 class。您只能导入已编译的 class 个文件。
如您在问题中的 link 中所述:
As we had learned in Chapter 8, one thing you’ll need to do is ensure that the Java class you wish to use resides within your CLASSPATH. In the example above, I created a JAR file that contained the Beach class and then put that JAR on the CLASSPATH.
编译是使用javac command which is part of JDK. Creating a jar文件完成的,文件是用JDK的命令jar命令部分完成的。
将不同文件的文件放在同一目录中不是一个好习惯。您应该为 .java 文件和单独目录中的 Jython 文件创建一个 Java 项目。
我正在试用 jython,很早就被第 10 章的示例卡住了 http://www.jython.org/jythonbook/en/1.0/JythonAndJavaIntegration.html java 库中的数学导入确实有效。但是我无法让自定义 class 海滩工作。
我看到了一个相同的 post Jython won't import user-defined class; ImportError: No module named ****** 但是我不明白接受的答案方法。
所以我希望得到一些澄清。
下面是我的项目结构。
import Beach
b = Beach("Cocoa Beach","Cocoa Beach")
print (b.getName(Beach.getName()))
导入海滩:未解决的导入:海滩
编辑:让它与以下内容一起工作:
import sys
sys.path.append('C:\Users\Rasmus\Desktop')
import Beach
beach = Beach("Cocoa Beach","Cocoa Beach")
beach.getName()
print(beach.getName())
但是 pydev eclipse 仍然将导入海滩标记为未解决的导入。
海滩class
public class Beach {
private String name;
private String city;
public Beach(String name, String city){
this.name = name;
this.city = city;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
}
您需要在 Jython class 路径中包含已编译的 class。您只能导入已编译的 class 个文件。
如您在问题中的 link 中所述:
As we had learned in Chapter 8, one thing you’ll need to do is ensure that the Java class you wish to use resides within your CLASSPATH. In the example above, I created a JAR file that contained the Beach class and then put that JAR on the CLASSPATH.
编译是使用javac command which is part of JDK. Creating a jar文件完成的,文件是用JDK的命令jar命令部分完成的。
将不同文件的文件放在同一目录中不是一个好习惯。您应该为 .java 文件和单独目录中的 Jython 文件创建一个 Java 项目。