4

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
  • where is ubicated add.java? Your code only works if add.java is in the same folder as javac Commented Feb 8, 2020 at 5:31
  • You tried the cd 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 (with cd), you can only access the files in that directory. So copy the add.java to the bin of java installation or use the absolute path to the add.java when calling javac. To avoid using absolute path you can firstly obtain the directory of add.java file, then use that variable before calling javac. The javac compiles to cwd, so calling java add should work. Commented Feb 8, 2020 at 5:51

2 Answers 2

4

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")
Sign up to request clarification or add additional context in comments.

7 Comments

Yeah It worked thanks! I also copied my script directly into my java folder and that worked too....
I'm happy to help you. Shame it costs me a downvote jajaja
If the asker says it worked, I can remove it. But IMO, it would be better to try adding that java folder to PATH instead of using absolute path, and as you see by asker comment - to explain why copying the java file to `\bin` folder is bad. I'll be able to remove the -1 later (due to SO limits) or if you edit your answer.
@itwasntme I agree with you. I didn't take much effort in my answer. May be I just thinked in a fast question that works but doesn't really make a teach. Leave the -1 its ok. I think you can make a good answer from your asker comment
Mmm, I'd need to install python and etc. Just put some additional info in yours then I'll remove the -1. Your code works in the 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.
|
0

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()

1 Comment

I recomend you that you may have your programs out of installations folders. If later you install new java version, you will have to copy your codes and change the script

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.