Gọi java từ quy trình con python
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 Show
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 –
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 –
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 chungmục tiêu 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
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ínhghi 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 # 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ẩuimport 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 EOFcmd_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) Làm cách nào để gọi Java bằng Python?ví dụ/trăn/java. p . từ jpype nhập startJVM, shutdownJVM, java #startJVM(getDefaultJVMPath(), "-ea") startJVM(convertStrings=False) java. lang thang. Hệ thống. ngoài. println("Xin chào thế giới") ngẫu nhiên = java. sử dụng. Ngẫu nhiên() số1 = ngẫu nhiên. nextInt(10) số2 = ngẫu nhiên. nextInt(10) in(số1, số2) Làm cách nào để chạy tập lệnh Python từ tập lệnh Python khác bằng quy trình con?Để thực thi mã Python khác, các đối số phải chứa đường dẫn đến tệp thực thi Python và tới tập lệnh Python . Bên trong args chúng ta có những điều sau đây. "/ usr/cục bộ/bin/trăn". đường dẫn đến tệp thực thi Python cục bộ. "-c". thẻ Python cho phép người dùng viết mã Python dưới dạng văn bản vào dòng lệnh.
Sự khác biệt giữa chạy quy trình con và cuộc gọi quy trình con là gì?Tôi có thể nói rằng bạn sử dụng quy trình con. call() khi bạn muốn chương trình chờ quá trình hoàn tất trước khi chuyển sang quá trình tiếp theo. Trong trường hợp quy trình con. run() , chương trình sẽ cố gắng chạy tất cả các quy trình cùng một lúc, chắc chắn sẽ khiến chương trình bị sập
Một quy trình con trong Java là gì?Quy trình con được bắt đầu với IO của nó chuyển sang quy trình đã thực thi nó . Điều này là cần thiết để xem cả bất kỳ thiết bị xuất chuẩn và thiết bị xuất chuẩn nào mà nó tạo ra. inheritIO là một phương thức tiện lợi và cũng có thể đạt được bằng cách gọi chuỗi mã sau thay vào đó (cũng định cấu hình stdin của quy trình con). người xây dựng. |