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
javac CreatingUser.java
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.
Related
Can you make a jar file out of this project that runs with no initializererror?
Project here.
https://github.com/piersy/BasicTesseractExample
I've tried using jarsplice to splice the project jar with the tesseract & javacpp jars and I get this initializer error, when running the jar in the command prompt. I don't know if jarsplice is even required but that was my effort.
C:\Users\user\Desktop\JavaWorkspace>java -jar fatOCR.jar Exception in thread "main" java.lang.ExceptionInInitializerError
at BasicTesseractExampleTest.givenTessBaseApi_whenImageOcrd_thenTextDisplayed(BasicTesseractExampleTest.java:14)
at runOCR.main(runOCR.java:7) Caused by: java.lang.ArrayStoreException: sun.reflect.annotation.TypeNotPresentExceptionProxy
at java.base/sun.reflect.annotation.AnnotationParser.parseClassArray(Unknown Source)
at java.base/sun.reflect.annotation.AnnotationParser.parseArray(Unknown Source)
at java.base/sun.reflect.annotation.AnnotationParser.parseMemberValue(Unknown Source)
at java.base/sun.reflect.annotation.AnnotationParser.parseAnnotation2(Unknown Source)
at java.base/sun.reflect.annotation.AnnotationParser.parseAnnotations2(Unknown Source)
at java.base/sun.reflect.annotation.AnnotationParser.parseAnnotations(Unknown Source)
at java.base/java.lang.Class.createAnnotationData(Unknown Source)
at java.base/java.lang.Class.annotationData(Unknown Source)
at java.base/java.lang.Class.createAnnotationData(Unknown Source)
at java.base/java.lang.Class.annotationData(Unknown Source)
at java.base/java.lang.Class.getAnnotation(Unknown Source)
at java.base/java.lang.reflect.AnnotatedElement.isAnnotationPresent(Unknown Source)
at java.base/java.lang.Class.isAnnotationPresent(Unknown Source)
at org.bytedeco.javacpp.ClassProperties.load(ClassProperties.java:128)
at org.bytedeco.javacpp.Loader.loadProperties(Loader.java:203)
at org.bytedeco.javacpp.Loader.load(Loader.java:372)
at org.bytedeco.javacpp.Loader.load(Loader.java:354)
at org.bytedeco.javacpp.tesseract$TessBaseAPI.<clinit>(tesseract.java:3422)
... 2 more
I don't think you can run this project because it contains only tests (1 test in this case) and does not contain the main method.
I write a Java program that read text from excel.So I import some jar like this: poi-3.10-beta2-20130904.jar, poi-ooxml-3.10-beta2-20130904.jar, etc., I can run the program correctly in eclipse.But when I package this program with maven to the directory(C:\workspace2\change\bin),
Then I run this program in command like this :
C:\workspace2\change\bin>java GenerateVar
it occurs this error:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/poi/ss/usermodel/Cell
at GenerateVar.execute(GenerateVar.java:59)
at GenerateVar.main(GenerateVar.java:25)
Caused by: java.lang.ClassNotFoundException: org.apache.poi.ss.usermodel.Cell
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
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)
... 2 more
You need to specify classpath with all other jars/classes you use in your program.
E.g. Setting multiple jars in java classpath
When you run it from eclipse in the console there's a command that could be used to run the project, you could copy/paste it from the console to your command line.
In the command java GenerateVar you didn't set -cp option. With this option the command line will look like
java -cp poi-3.10-beta2-20130904.jar poi-ooxml-3.10-beta2-20130904.jar ... GenerateVar
Assumed the libraries are in the current dir.
How to use maven plugins to add dependency to your project and build jar you can find here.
See also this answer if you want to modify manifest.mf manually.
over, I change the way do what I want to do. I use the fat jar which is the eclipse plugin to package the program
I am trying to execute the performance-meters.jar according to the tutorial at
http://marklogic.github.io/performance-meters/tutorial.html
using the command
java -cp performance-meters.jar:xcc.jar com.marklogic.performance.PerformanceMeters
I am getting the following error:
D:\MBS\performance-meters-master\performance-meters-master\classes>java -cp performance-meters.jar:xcc.jar com.marklogic.performance.PerformanceMeters
Exception in thread "main" java.lang.NoClassDefFoundError: com/marklogic/performance/PerformanceMeters
Caused by: java.lang.ClassNotFoundException: com.marklogic.performance.PerformanceMeters
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
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)
Could not find the main class: com.marklogic.performance.PerformanceMeters. Program will exit.
I have kept both the jar files in the same folder.
Links to the jar files are:
http://developer.marklogic.com/code/performance-meters
[http://developer.marklogic.com/products/xcc][3]
Any help would be appreciated.
try to use semicolon instead colon in your cp declaration.
java -cp performance-meters.jar;xcc.jar com.marklogic.performance.PerformanceMeters
You are using windows system or Unix? If windows then I think you need to use ; and not : in separating jars as follows:
java -cp performance-meters.jar ; xcc.jar com.marklogic.performance.PerformanceMeters
This question already has an answer here:
How come command line isn't recognizing this java command?
(1 answer)
Closed 10 years ago.
I've tried searching around, but I couldn't find any answers that fit my case.
I can run the file CB.java just fine when I use an IDE. This file depends on classes specified in cs2.jar. Here are the contents of its directory.
02/12/2013 03:43 PM <DIR> .
02/12/2013 03:43 PM <DIR> ..
02/12/2013 03:45 PM 2,226 CB.class
02/12/2013 01:21 PM 2,164 CB.java
02/12/2013 03:43 PM 71,128 cs2.jar
3 File(s) 75,518 bytes
2 Dir(s) 408,977,362,944 bytes free
When I run it off my IDE, CB.java works just fine. However, when I try java CB in the command line, I get:
Exception in thread "main" java.lang.NoClassDefFoundError: sn/visual/JRect
angle
Caused by: java.lang.ClassNotFoundException: sn.visual.JRectangle
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
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)
Could not find the main class: CB. Program will exit.
Furthermore, I tried following suggestions to add something to the classpath using:
>java -cp C:\Users\...blah blah blah...\Software_Engineering cs2
Exception in thread "main" java.lang.NoClassDefFoundError: cs2
Caused by: java.lang.ClassNotFoundException: cs2
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
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)
Could not find the main class: cs2. Program will exit.
How come my IDE is smart but my command line isn't?
Thank you.
The class path is set to just consider .class files in the given directory. You need to add the jar file to the class path: java -cp C:\somewhere\cs2.jar
How come my IDE is smart but my command line isn't?
I suspect in your IDE, you've included the jar file in your build path, so it then includes it when both building and running. (You haven't told us which IDE it is, so it's hard to use the exact terminology it would use)
On the command line, you need to specify the jar file when both building and running too, so you'd use:
To build:
javac -cp cs2.jar CB
To run:
java -cp .;cs2.jar CB
i'm trying to run testng tests using command line like this :
C:\Documents and Settings\Administrateur\Bureau\automatic tests testNG>java -cp
C:\Documents and Settings\Administrateur\.m2\repository\org\testng\testng\6.3.1\
testng-6.3.1.jar org.testng.TestNG testng.xml
but i'm getting this error :
Exception in thread "main" java.lang.NoClassDefFoundError: and
Caused by: java.lang.ClassNotFoundException: and
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
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)
Could not find the main class: and. Program will exit.
So How to add testNG to the classpath.
Thanks
The spaces in the filename are confusing things - Java thinks that only the c:\Documents part is in the classpath argument. Try this:
java -cp
"C:\Documents and Settings"\Administrateur\.m2\repository\org\
testng\testng\6.3.1\testng-6.3.1.jar org.testng.TestNG testng.xml
(where the whole thing is on one line and there's no break after "org" of course)
If you want to run via java on command line, you'll need to either enquote your path ("C:\Documents and Settings\Administrateur\.m2\repository\org\testng\testng\6.3.1\") or alter slightly as follows: C:\Documents%20and%20Settings\Administrateur\.m2\repository\org\testng\testng\6.3.1\. You may also want to run via maven to avoid having to specify this classpath info.