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
2 Answers 2
I think the problem is that your current directory may not contains add.java after doing cd C:\\Program Files\\Java\\jdk-13.0.1\\bin:
You may try this "static solution" that works for one java instalation:
import os
import time
#import subprocess
os.system("cls")
time.sleep(2)
os.system("C:\\Program Files\\Java\\jdk-13.0.1\\bin\\javac add.java")
os.system("C:\\Program Files\\Java\\jdk-13.0.1\\bin\\java add")
You may also include your java installation path in the PATH of the operative system, and you may run javac and java without their absolute path. If you later change the java version, with only updating the java path the script will maintain operable. In this case the code will be like this:
import os
import time
#import subprocess
os.system("cls")
time.sleep(2)
os.system("javac add.java")
os.system("java add")
7 Comments
cwd where the program starts - so using it, there's no need to copy the .java file to the bin. I do it later, cause now I need to go to sleep for a bit.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(f"javac {name}.java")
os.system(f"java {name}")
key = input()
add.java? Your code only works if add.java is in the same folder as javaccd java path, what do you think it suppose to do? It's not a command to add the directory to the PATH variable. Also, when you change the current directory (withcd), you can only access the files in that directory. So copy theadd.javato the bin of java installation or use the absolute path to theadd.javawhen callingjavac. To avoid using absolute path you can firstly obtain the directory ofadd.javafile, then use that variable before callingjavac. The javac compiles tocwd, so callingjava addshould work.