我想从位于磁盘上的file.java加载类定义或字段。 我试过URLClassLoader:
import swiftclient
import os
import sys
import subprocess
user = 'user'
key = 'key'
url = "url"
conn = swiftclient.Connection(
user=user,
key=key,
authurl=url)
input_file_name = "test.txt"
def write_error_log(path):
"""
Input : file path
This module writes the path which is failed to create or write during
downloading.
"""
with open('log.txt', 'w') as f:
f.write("Output not created for " + path)
def main():
"""
Main method is used to read the entire text file and
creates the file and folder based on the returned values
during reading.
It creats an error log file when script fails to create
the file.
"""
for path in open(input_file_name, 'r'):
print("Writing file", path)
download_command = "swift -A {0} -U {1} -K {2} download {3} {4}".format(url, user, key, container_name, path)
try:
return_status_code = subprocess.call(download_command, shell=True)
if return_status_code:
write_error_log(path)
except:
write_error_log(path)
if __name__ == "__main__":
container_name = sys.argv[1]
main()
但它不起作用;(
答案 0 :(得分:0)
您可以通过loadClass
加载课程,但不能加载源代码文件。 .java
个文件包含源代码。使用Java,与许多(但不是全部)其他语言一样,您必须将源代码“编译”为机器可读的形式 - 在Java的情况下,是.class
文件(可能会也可能不会捆绑成.jar
)。您可以使用javac
工具(或IDE的功能)编译Java代码。
例如,如果您在名为Foo
的文件中定义了名为Foo.java
的类,则:
javac Foo.java
...将创建Foo.class
,然后可以加载。
您可能会发现this "Getting Started": The "Hello World" application tutorial from Oracle有用。它完成了在计算机上设置工具,编写源文件,将其编译为类文件以及运行它的步骤。