debug java jar file from command line? - java

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.

Related

Unable to run Java main class though windows commandline

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

Failed to run java in remote command line with external jar file

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

Unable to execute java program in jenkins?

I am trying to execute simple java class through jenkins and i have followed below post but no results,please help how to execute complete java program in jenkins.
How to compile and run a simple java file in jenkins on Windows
Always getting error as
Building in workspace C:\Program Files (x86)\Jenkins\workspace\Run Java Class
[Run Java Class] $ cmd /c call C:\Windows\TEMP\hudson7739288570136427109.bat
C:\Program Files (x86)\Jenkins\workspace\Run Java Class>javac "E:\Java_Tutorials\JavaPractices\src\com\practice\javsSuperCars.java"
javac: file not found: E:\Java_Tutorials\JavaPractices\src\com\practice\javsSuperCars.java
Usage: javac <options> <source files>
use -help for a list of possible options
C:\Program Files (x86)\Jenkins\workspace\Run Java Class>java SuperCars
Error: Could not find or load main class SuperCars
C:\Program Files (x86)\Jenkins\workspace\Run Java Class>exit 1
Build step 'Execute Windows batch command' marked build as failure
Finished: FAILURE
The post that you referred was a sample example where the Java class that was executed was on the Jenkins Workspace folder directly, but your application is different as it is with a project folder structure.
Jenkins doesn't identify your Main class without you adding it to the CLASSPATH implicitly. Here the necessity of setting the CLASSPATH is more important.
In the Execute Windows batch Command section, you need to traverse to your Main class and then you need to issue the command to compile the same.
Hope this helps!

java.lang.NoClassDefFoundError when running my java program

i'm new with java.
when i come to run my java app i'm getting this error below:
java.lang.NoClassDefFoundError: org/apache/poi/EncryptedDocumentException
the issue it is ALSO happens when i'm adding the classpath to my command:
java -jar myApp.jar -classpath .\lib
BUT,
when i'm copy myApp.jar to the lib direcotry and running form there it's working fine.
thanks for the help guys.
You cannot use both -jar and -classpath at the same time. If you use -jar, it will ignore your -classpath and use settings from the manifest in the jar. Try all in the classpath:
java -cp myApp.jar:lib/*:. mypackage.MyClass
On Windows you need to use ';' instead of ':'
java -cp myApp.jar;lib/*;. mypackage.MyClass
See similar question here: Execute jar file with multiple classpath libraries from command prompt

Classpath contains only the running file

I'm having issues trying to use the Connector/J JDBC driver in my code.
The code I have written uses the Class.forName("com.mysql.jdbc.Driver").newInstance() to load the class before DriverManager.getConnection() is used to load the driver.
This results in the ClassNotFoundException for com.mydql.jdbc.Driver. I have the binary JAR file for the mysql connector, mysql-connector-java-5.1.26-bin.jar.
My code is packaged into a JAR file by building in Netbeans.
To run the code I am using the following
java -classpath "/path/to/mysql-connector-java-5.1.26-bin.jar" -jar MyJarFile.jar
This gives the exception: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
Adding System.out.println(System.getProperty("java.class.path")); to the beginning of the program prints only MyJarFile.jar.
I have tried adding the jar file to the CLASSPATH variable using export, and setting the last part of the -classpath flag to lib/*, but with the same results.
I tried running the program from a .class file, instead. It complained about being unable to find or load the main class. It would run only when both the wildcard was used in the classpath and the MyJarFile.jar was in that location. It would simply hang at the loading of the Driver, though.
Does anyone have any thoughts as to what is going on?
Try don't mix -cp and -jar options, this might work:
java -cp "mysql-connector-java-5.1.26-bin.jar:MyJarFile.jar" my.package.Main
for *nix or
java -cp "mysql-connector-java-5.1.26-bin.jar;MyJarFile.jar" my.package.Main
for windows
where my.package.Main is your main class.

Categories