I'm trying to remotely debug a jar file bundled with dependencies created with maven-assembly-plugin. But everything I do, and no matter how I call the debugger on the machine running the code (or on my local machine for testing) I always get a "Could not find main class". That's the message even if I specify the exact main class in the command line.
POM file:
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.xxx.yyy.MyMainClass</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
I'm creating the jar with dependencies with:
clean compiler:compile assembly:single
The debugging is started with:
java -Xdebug -Xrunjdwp:transport=dt_socket,address=8800,server=y,suspend=y -classpath %classpath com.xxx.yyy.MyMainClass pubsubtest-0.1-jar-with-dependencies.jar
The application starts and says:
Listening for transport dt_socket at address: 8800
But when I connect to the port with Netbeans the issue is:
Error: Could not find or load main class com.xxx.yyy.MyMainClass
Am I missing something? Should I setup Netbeans somehow? In Netbeans I'm keeping the project open.
I followed this tutorial:
https://blogs.oracle.com/atishay/entry/use_netbeans_to_debug_a
Related
I am trying to deploy a maven project in minishift using the openjdk8 source to image strategy. The application is built and deployed, however, it fails at run-time with the following error:
Starting the Java application using /opt/run-java/run-java.sh ...
exec java -javaagent:/opt/jolokia/jolokia.jar=config=/opt/jolokia/etc/jolokia.properties -XX:+UseParallelGC -XX:MinHeapFreeRatio=20 -XX:MaxHeapFreeRatio=40 -XX:GCTimeRatio=4 -XX:AdaptiveSizePolicyWeight=90 -XX:MaxMetaspaceSize=100m -XX:+ExitOnOutOfMemoryError -cp . -jar /deployments/app-1.0-SNAPSHOT.jar
no main manifest attribute, in /deployments/app-1.0-SNAPSHOT.jar
I have this on my pom.xml
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<archive>
<manifest>
<mainClass>com.mypackage.MyClass</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
I thought adding the manifest entry on the maven pom was enough.
Do I have to provide a separate manifest file?
Do I have to provide extra VM argument to specify the main class? if so, how do I do that in openshift?
Any other better alternative?
Add a new environment variable JAVA_MAIN_CLASS to your deployment config or via the Openshift user interface.
JAVA_MAIN_CLASS=com.mypackage.MyClass
That should resolve the issue.
i am using netbeans 8.2 to create a simple java project. i've chosen Maven-Java Application. I've created a simple hello world java class. I've selected the java class as the main class from Properties -> Run and if i run the project from Netbeans, it print the hello world. After that, i do "Clean" and "Build with dependencies" the editor create a target folder with the jar file. When i go to that folder and execute "java -jar XXX.jar", i get the no main manifest attribute error. Do I have to manually update the POM.xml file? Am I missing a step?
I am answering my own question.
Apparently you have to manually update the pom.xml file. I've added the dependency for maven-jar-plugin and added below section to the pom.xml file.
<build>
<plugins>
<plugin>
<!-- Build an executable JAR -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.mypackage.XXX</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
I have the following configuration for my maven build and I have double checked the class name as well as package name multiple times to ensure it's accuracy. But everytime I run:
java -jar <snapshot-with-dependencies>.jar I get Error: Could not find or load main class com.atlassian.JiraRestCaller.
The excerpt from my pom file is as below
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.atlassian.JiraRestCaller</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
I also tried adding <sourceDirectory>src/main/java/com/atlassian/</sourceDirectory> but still get the same error
Solution1:
I spent a decent amount of time trying to solve this problem. I thought that I was somehow setting my classpath incorrectly but the problem was that I typed:
java -cp C:/java/MyClasses C:/java/MyClasses/utilities/myapp/Cool
instead of:
java -cp C:/java/MyClasses utilities/myapp/Cool
I thought the meaning of fully qualified meant to include the full path name instead of the full package name.
Solution2:
If you use Maven to build the JAR file, please make sure to specify the main class in the pom.xml file:
<build>
<plugins>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>class name us.com.test.abc.MyMainClass</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
This might help you if your case is specifically like mine: as a beginner I also ran into this problem when I tried to run a Java program.
I compiled it like this:
javac HelloWorld.java
And I tried to run also with the same extension:
java Helloworld.java
When I removed the .java and rewrote the command like java HelloWorld, the program ran perfectly. :)
So we had this today
[myproject]-[master] $ mvn
[MVNVM] Using maven: 3.5.2
Error: Could not find or load main class html
and we had an issue with Proxies.
Check your MAVEN_OPTS and make sure that if you are sending in a proxy to maven, that it exists and you can use it.
MAVEN_OPTS=-Dhttp.proxyHost=www-proxy.myproxyprovider.com -Dhttp.proxyPort=80 -Dhttps.proxyHost=www-proxy.myproxyprovider.com -Dhttps.proxyPort=80
or if it is set and you shouldnt have one, then get rid of it.
[myproject]-[master] $ mvn -version
[MVNVM] Using maven: 3.5.2
Apache Maven 3.5.2 (138edd61fd100ec658bfa2d307c43b76940a5d7d; 2017-10-18T08:58:13+01:00)
Maven home: /Users/bamcgill/.mvnvm/apache-maven-3.5.2
I experienced the same error. I fixed it by upgrading from Maven 3.3.3 to Maven 3.6.3. I am not sure whether that fix is related to this question, because I did not debug my issue.
I know that there's a lot of question related to this issue but none made sense for me. I built a Java Desktop application that adds products for sale by communicating with an API. I am using okhttp 3.9.0 to accomplish this task. The problem is that my app works just fine when I execute the Netbeans' "run project" command but when I use the "java -jar file.jar" command to run the app I get exceptions stating that okhttp3 classes weren't found. That's the first time I try to run a Java application outside of an IDE so I kind of lost.
I am running my app from:
C:\Users\Diego Alves\.m2\repository\com\mycompany\loja\1.0-SNAPSHOT
Also, something that bugs me is that when searching for okhttp3 I generally end up on an Android-related page. Isn't okhttp3 used for desktop apps?
You need to add dependencies to your jar(okhttp is one of them) so that the JVM can find them in your classpath. To do that, in pom.xml add the following plugin:
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>your.package.MainClass</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
Also make sure you have set <packaging>jar</packaging>
Then run the build (mvn clean install), it will generate a jar that can be executed successfully.
java -jar target/your_jar_name.jar
For OkHTTP there is no link with android, you can use it in any Java Application.
I am trying to change where any error files for a java program will be output to. From what I have researched and gather the following seems correct java -XX:ErrorFile=/foo -jar foo.jar. However since my program does not error so no dump file is created (the error file specification is there in case any ever happens) I am not sure if the syntax is correct. Does the syntax look correct for changing the output of the error file then running the jar file I want, or will an issue be caused? Currently running 1.7.65 on Red Hat 6.5
You cannot use -classpath with -jar option
However you can use maven to have the dependencies for you jar like this
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>well.defined.path.of.the.mainClass</mainClass>
</manifest>
</archive>
</configuration>
</plugin>