run_java.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. r"""P12-B(续):Java 后端启动器(灰度期与 Python 并存,端口 28120)。
  4. 用法:python app_backEnd/backend-java/run_java.py [--port 28120]
  5. 先 `python build_offline.py` 生成 target/classes 与 target/lib,再用 `java -cp` 起(离线,不依赖 mvn 打包)。
  6. """
  7. from __future__ import annotations
  8. import argparse
  9. import os
  10. import pathlib
  11. import subprocess
  12. import sys
  13. HERE = pathlib.Path(__file__).resolve().parent
  14. JAVA = pathlib.Path(r'D:\Program Files\Java\jdk1.8.0_202\bin\java.exe')
  15. def main() -> int:
  16. ap = argparse.ArgumentParser()
  17. ap.add_argument('--port', default='28120')
  18. a = ap.parse_args()
  19. classes = HERE / 'target/classes'
  20. lib = HERE / 'target/lib'
  21. if not classes.exists():
  22. print('缺少 %s —— 先跑 build_offline.py' % classes)
  23. return 2
  24. cp = os.pathsep.join([str(classes)] + ([str(lib / '*')] if lib.exists() else []))
  25. exe = JAVA if JAVA.exists() else 'java'
  26. env = dict(os.environ, SERVER_PORT=a.port)
  27. print('启动:', exe, '-cp', cp[:80] + '…', 'GuanlanBackendApplication')
  28. return subprocess.call([str(exe), '-Dfile.encoding=UTF-8', '-cp', cp,
  29. 'com.guanlan.backend.GuanlanBackendApplication'], cwd=str(HERE), env=env)
  30. if __name__ == '__main__':
  31. raise SystemExit(main())