简体   繁体   中英

Run a batch file in Python which contains java command

It is quite frustrating when you can't execute a .bat file in python while it is executing manually.

I am attaching my code here:

directory = 'E:/'
with open(os.path.join(directory, 'output_file.bat'), 'w') as OPATH:
    OPATH.writelines(['"""',"\n"'E:',"\n",
                      'javacCreatingUser.java',"\n",'javaCreatingUser',"\n",'"""'])

os.system("E:/output_file.bat")

The above is my python code which is creating a bat file with 2 java command

  1. javac CreatingUser.java
  2. java CreatingUser

I can run the .bat file manually and it is working fine but my python script is giving me the following error:

java.lang.NoClassDefFoundError: oracle/iam/identity/exception/ValidationFailedException
    at java.lang.Class.getDeclaredMethods0(Native Method)
    at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
    at java.lang.Class.privateGetMethodRecursive(Unknown Source)
    at java.lang.Class.getMethod0(Unknown Source)
    at java.lang.Class.getMethod(Unknown Source)
    at sun.launcher.LauncherHelper.validateMainClass(Unknown Source)
    at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Caused by: java.lang.ClassNotFoundException: oracle.iam.identity.exception.ValidationFailedException
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 7 more
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" '"""' is not recognized as an internal or external command,

operable program or batch file.

The question is that if some classes are missing in my file then it should not execute manually as well, but manual execution is fine.

javac CreatingUser.java
java CreatingUser

This is the source of your problem: You're only compiling one class file and then calling that class without a classpath.

Java has a search path for classes comparable to Pythons PYTHONPATH called classpath .

You can give this a try:

javac CreatingUser.java
java -classpath YOUR_CLASSPATH CreatingUser

YOUR_CLASSPATH can be a colon (Linux) or semicolon (Windows) separated list of JAR files and directories containing class files. Let your classpath point to the JARs you're needing and you're fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM