Tệp jar là một gói nén gồm các tệp lớp Java và siêu dữ liệu và tài nguyên được liên kết [văn bản, hình ảnh, v.v. ] thành một tệp để phân phối phần mềm ứng dụng hoặc thư viện trên nền tảng Java. Nếu bạn đang sử dụng ngôn ngữ lập trình java thì bạn có thể dễ dàng chạy nó. Nhưng chạy trên ngôn ngữ lập trình Python thì sao? . Hãy hiểu chúng
Gọi tệp jar bằng Python –
1. Sử dụng mô-đun Sub process –
Tôi thấy quy trình con là cách tốt nhất để gọi bất kỳ jar nào từ python. Về cơ bản, Nếu tôi nói, Đó là một trong những cách tốt nhất để gọi bất kỳ gói ngôn ngữ chéo nào khác
Cú pháp –
subprocess.call[['java', '-jar', 'jarname.jar', arg_variable1, arg_variableN]]
Giải trình-
Khi cú pháp làm rõ, mỗi giá trị được cung cấp trong các đối số sẽ được phân tách bằng dấu phẩy. Nếu đó là một chuỗi giống như tên được mã hóa cứng như jar, v.v. Nó sẽ nằm trong dấu ngoặc kép nếu không nó sẽ giống như một biến đơn giản
2. Sử dụng mô-đun hệ điều hành –
Sử dụng mô-đun Os cũng là một cách khác để gọi tệp jar bên trong mã python/script. Đây là cú pháp cơ bản cho việc này –
Cú pháp –
os.system[ "java -jar jarname.jar " +arg_variable1 + " " +arg_variableN]
Giải trình -
Không giống như mô-đun quy trình con, hệ điều hành. hệ thống không lấy các đối số được phân tách bằng dấu phẩy. Ở đây chúng ta phải cung cấp không gian để phân biệt giữa các đối số
3. Quy trình con nào tốt hơn. gọi hệ điều hành VS. hệ thống -
Theo tài liệu chính thức của Python 3 và kinh nghiệm cá nhân của tôi, tôi thấy quy trình con là cách tốt hơn để gọi các tệp jar bằng Python. Để chứng minh điều này, tôi đính kèm hình ảnh đã cắt từ tài liệu chính thức của python 3. Làm ơn hãy kiểm tra nó -
mục tiêu và lời khuyên chung
mục tiêu
Mô-đun quy trình con cho phép bạn sinh ra các quy trình mới, kết nối với các đường dẫn đầu vào/đầu ra/lỗi của chúng và lấy mã trả về của chúng
Mô-đun này dự định thay thế một số mô-đun và chức năng cũ hơnos.system
os.spawn*
phương pháp được đề xuất. sử dụng chức năng run[]
cho tất cả các trường hợp sử dụng mà nó có thể xử lý. Đối với các trường hợp sử dụng nâng cao hơn, giao diện
subprocess.run[["dir", "/S"], shell=True, check=True] print['we see that message'] subprocess.run[["ddir", "/S"], shell=True, check=True] print["we don't see that message"]0 bên dưới có thể được sử dụng trực tiếp
chức năng chính
run[]
Chạy lệnh được mô tả bởi args. Đợi lệnh hoàn thành, sau đó trả về một phiên bản CompletedProcess
subprocess.run[["dir", "/S"], shell=True, check=True] print['we see that message'] subprocess.run[["ddir", "/S"], shell=True, check=True] print["we don't see that message"]2
subprocess.run[["dir", "/S"], shell=True, check=True] print['we see that message'] subprocess.run[["ddir", "/S"], shell=True, check=True] print["we don't see that message"]3 Thực hiện một chương trình con trong một quy trình mới
trường hợp sử dụng chính
ghi chú. Trước Python 3. 5, ba chức năng này.
subprocess.run[["dir", "/S"], shell=True, check=True] print['we see that message'] subprocess.run[["ddir", "/S"], shell=True, check=True] print["we don't see that message"]4,
subprocess.run[["dir", "/S"], shell=True, check=True] print['we see that message'] subprocess.run[["ddir", "/S"], shell=True, check=True] print["we don't see that message"]5,
subprocess.run[["dir", "/S"], shell=True, check=True] print['we see that message'] subprocess.run[["ddir", "/S"], shell=True, check=True] print["we don't see that message"]6 bao gồm API cấp cao để xử lý con. Bây giờ bạn có thể sử dụng
run[]
trong nhiều trường hợpCác trường hợp sử dụng chính
– Chạy một lệnh, đợi hoàn thành và trả về mã thoát
subprocess.run[["dir", "/S"], shell=True, check=True] print['we see that message'] subprocess.run[["ddir", "/S"], shell=True, check=True] print["we don't see that message"]8[trước khi chúng tôi sử dụng
subprocess.run[["dir", "/S"], shell=True, check=True] print['we see that message'] subprocess.run[["ddir", "/S"], shell=True, check=True] print["we don't see that message"]9]
Thí dụ
# for windows command we need use the shell to True return_code: int = subprocess.run[["ddir", "/S"], shell=True].returncode print[f'return_code={return_code}'] return_code: int = subprocess.run[["dir", "/S"], shell=True].returncode # return_code: int = subprocess.run[["ls", "-l"]].returncode print[f'return_code={return_code}']
-Chạy một lệnh, đợi lệnh hoàn thành, mong đợi thành công và chụp thiết bị xuất chuẩn hoặc thiết bị xuất chuẩn
completed_process: subprocess.CompletedProcess = subprocess.run[["dir", "/S"], shell=True, check=True, stdout=subprocess.PIPE] print[f'output={completed_process.stdout}'] completed_process: subprocess.CompletedProcess = subprocess.run[["ddir", "/S"], shell=True, check=True, stdout=subprocess.PIPE] print['we never reach this statement']0 [trước khi chúng tôi sử dụng
completed_process: subprocess.CompletedProcess = subprocess.run[["dir", "/S"], shell=True, check=True, stdout=subprocess.PIPE] print[f'output={completed_process.stdout}'] completed_process: subprocess.CompletedProcess = subprocess.run[["ddir", "/S"], shell=True, check=True, stdout=subprocess.PIPE] print['we never reach this statement']1]
Thí dụ
subprocess.run[["dir", "/S"], shell=True, check=True] print['we see that message'] subprocess.run[["ddir", "/S"], shell=True, check=True] print["we don't see that message"]
-Chạy một lệnh, đợi lệnh hoàn thành, mong đợi thành công và trả về kết quả của nó
completed_process: subprocess.CompletedProcess = subprocess.run[["dir", "/S"], shell=True, check=True, stdout=subprocess.PIPE] print[f'output={completed_process.stdout}'] completed_process: subprocess.CompletedProcess = subprocess.run[["ddir", "/S"], shell=True, check=True, stdout=subprocess.PIPE] print['we never reach this statement']2 [trước khi chúng tôi sử dụng
completed_process: subprocess.CompletedProcess = subprocess.run[["dir", "/S"], shell=True, check=True, stdout=subprocess.PIPE] print[f'output={completed_process.stdout}'] completed_process: subprocess.CompletedProcess = subprocess.run[["ddir", "/S"], shell=True, check=True, stdout=subprocess.PIPE] print['we never reach this statement']1]
Thí dụ
completed_process: subprocess.CompletedProcess = subprocess.run[["dir", "/S"], shell=True, check=True, stdout=subprocess.PIPE] print[f'output={completed_process.stdout}'] completed_process: subprocess.CompletedProcess = subprocess.run[["ddir", "/S"], shell=True, check=True, stdout=subprocess.PIPE] print['we never reach this statement']
-chạy một lệnh và không đợi nó hoàn thành, thay vào đó hãy chạy lệnh như một tiến trình con
Thí dụ
# creationflags is optional subprocess.Popen[["C:\\Program Files\\Talon\\python.exe", "C:\\Program Files\\Talon\\repl.py"], creationflags=subprocess.CREATE_NEW_CONSOLE]
nhập khẩu
import subprocess
Ví dụ với API cũ
subprocess.call['ls -lrt /tmp', shell=True]
Các lệnh kéo dài trên một số dòng với EOF
cmd_sql_plus_oracle: str = [ "sqlplus -s -l / as sysdba >> EOF" "nCREATE OR REPLACE DIRECTORY fooDir AS '/apps/oracle/fooDir';" "nGRANT READ, WRITE ON DIRECTORY fooDir TO foo_user;" "nexit; " "nEOF" ] subprocess.call[cmd_sql_plus_oracle, shell=True]