I have an external jar and I have written 2 classes named 'TestConnector.java' and 'APIExampleUsageTestData.java' and I have placed these 2 files under default package.
I have placed all the 3 artifacts -> externalConnector.jar, TestConnector.java and APIExampleUsageTestData.java into a folder named 'temp'.
I have opened commandline in the temp folder context and compiled the class files with below command and it executed without any errors:
javac -cp "externalConnector.jar" *.java
I can see that there are 2 class files present in the tem folder.
There is a main method in the APIExampleUsageTestData.class and I'm trying to call that class with the below command, but, is failing
When running the below command in 'C:\Users\AppData\Local\Temp\0.15429262750877082' folder context
java -cp "externalConnector.jar" APIExampleUsageTestData
I'm getting error Error: Could not find or load main class APIExampleUsageTestData
When I trying to run the command, without any class path entry to check what error it is throwing:
java APIExampleUsageTestData
It throws error: Error: A JNI error has occurred, please check your installation and try again Exception in thread "main" java.lang.NoClassDefFoundError: com/external/connector/CustomException
It is a Windows Operating System. Could you please let me know how to run the program successfully?
You need both the current directory and the jar file to be on the classpath. So you want (Linux / Mac):
java -cp .:externalConnector.jar APIExampleUsageTestData
Or (Windows)
java -cp .;externalConnector.jar APIExampleUsageTestData
Related
I have a shell script which runs a java class presnet in /opt/mydir/ directory. In this script I am adding the class file along with few other jar files to the java classpath.
Ex:
/usr/bin/java -cp "/opt/mydir"
When the script is run, the java .class file fails with
Exception in thread "main" java.lang.NoClassDefFoundError: com/internal/altsite
The com.inetrnal.altsite comes from an external jar, and is also present in the same classpath as the main class file.
If i add the name of the jar explicitly to the classpath, the shell script runs successfully without any error. Ex:
/usr/bin/java -cp "/opt/mydir:/opt/mydir/altsite.jar:"
The directory structor is as follows:
-/opt
---mydir/
-------runner.class,altsite.jar,jose4j.jar
I cant execute java jar file from cmd windows 10.
I have generated a jar file using Intellji.
when I run the command Java -jar I got this error
Error: Unable to access jarfile
I tried running the cmd with administration right but same error. the class path is set correctly. the jar file is located in the right directory .
What could be causing the problem?
Thank you
make sure you use
java -jar TestJar.jar
instead of
java -jar TestJar
If this is not your error, the file might not exist or you donĀ“t have permission to open it.
I cannot get command-line debugging with jdb to work on a Cucumber/Java jar file. I looked at this SO question for example, and so far nothing has worked.
When I run the cucumber feature file from IntelliJ, it works just fine.
I compiled the program according to the local directions, and that finished successfully.
When I run the compiled jar file with java -jar runJar.jar, everything works just fine.
When I run jdb runJar.jar, I get the jdb prompt:
Initializing jdb ...
but when I try to run the Main-Class, it fails:
> run MainRunner
run MainRunner
Set uncaught java.lang.Throwable
Set deferred uncaught java.lang.Throwable
>
VM Started: Error: Could not find or load main class MainRunner
The application exited
I know that "MainRunner" is the correct class because that's the name of the "Main class:" in IntelliJ and also because I unzipped the jar file using this command:
unzip -c MyJar.jar META-INF/MANIFEST.MF | grep 'Main-Class
and it reports Main-Class: com.XXXXX.sdt.framework.runner.MainRunner as the Main-Class.
If instead run MainRunner I type run com.XXXXX.sdt.framework.runner.MainRunner, then I get the error:
VM Started: Error: Could not find or load main class com.XXXXX.sdt.framework.runner.MainRunner
If I startup a JVM and then attach to it with:
java -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=1044 -jar runJar.jar
and
jdb -attach 1044
and resume after the main1 prompt, I get the error
Exception occurred: java.lang.StringIndexOutOfBoundsException (uncaught)"thread=main", java.lang.String.charAt(), line=658 bci=21
The Java docs on jdb imply that this should just work, so I'm not sure where to go from here.
Did you notice that you are running runJar.jar but checked for the Main class in MyJar.jar?
If you are using a JAR file as an application packaging, you need the Main-Class in that jar file, not in one of the jar files on your path.
I want to run my Java code in remote server with external jar file. I referred to this tutorial and run IntelliJ IDE to generate a jar file of my whole project. Now I can run the following code in local terminal successfully.
javac -cp /Users/natsuapo/out/artifacts/main_jar/main.jar: new_server_try.java
java -cp /Users/natsuapo/out/artifacts/main_jar/main.jar: new_server_try
The code will run successfully. However, when I try the same thing in server. The compile process can be finished without any errors or warnings.
javac -cp /Users/natsuapo/out/artifacts/main_jar/main.jar: new_server_try.java
I have checked that the class file new_server_try.class is generated in the directory.
However the second step will return error as Could not find or load main class new_server_try and I am not sure why this happens.
on the second command try giving the full package name .. like shown below
java -cp "/Users/natsuapo/out/artifacts/main_jar/main.jar:lib/*" my.package.MainClass
also with operating system the command differs, check below
Windows
java -cp "Test.jar;lib/*" my.package.MainClass
Unix
java -cp "Test.jar:lib/*" my.package.MainClass
I have a .java file which i compiled in a package named "Mypack",using command line as follows
javac -d . file_name.java // The "." specifies the current working directory which was the desktop
so it creates a folder on the desktop named "Mypack"(The package name), in the folder the .class file for my program is placed.Now i did the following
java -classpath "C:\Users\LoRd CuRZon\Desktop\Mypack" file_name // Error Could not find or load main method
Even if i go into the directory "Mypack" and launch command prompt from that directory and try to run the program i still get the same error.
run it as likewise from Desktop,
c:/.../Desktop> java Mypack.file_name
java command requires fully qualified name .
so from desktop run java Mypack.classname
If you have this error:
Error Could not find or load main method
That means you don't write a main method in you code try to write it.
But befor to do those steps:
Fo compiling a programme do this:
java Mypack.file_name
To run it do like this:
java Mypack.classname