I am unable to compile and run a java program using python script

Aditya :

I think when I use os.system("cd java path") to change the path to java directory it's just don't change the path to that directory... Here's the code I have written:

    import os
    import time
    #import subprocess
    os.system("cls")
    os.system("cd C:\\Program Files\\Java\\jdk-13.0.1\\bin")
    time.sleep(2)
    os.system("javac add.java")
    os.system("java add")

Error:

error: file not found: add.java Usage: javac use --help for a list of possible options Error: Could not find or load main class add Caused by: java.lang.ClassNotFoundException: add

Aditya :

This code worked for me and I had to copy script into the bin folder to make it work..

  import os
  import time
  aditya = True
  while aditya:
      os.system("cls")
      print("Enter a program name to execute:")
      name = input()
      os.system("cd C:\\Program Files\\Java\\jdk-13.0.1\\bin")
  """Note : Code works without above statement(cd path)because it does nothing since 
   we are in same directory."""
     time.sleep(0.5)
     os.system(f"javac {name}.java")
     os.system(f"java {name}")
     key = input()

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=6122&siteId=1