Unable to build jar project - java

I need help with building jar file. I always getting message Error: Could not find or load main class Main when trying to execute jar file.
My MANIFEST.MF here:
Manifest-Version: 1.0
Main-Class: Main
And my Main.java file:
public class Main {
public static final Charset ISO_8859_1 = StandardCharsets.ISO_8859_1;
public static final Charset UTF_8 = StandardCharsets.UTF_8;
public static void main(String[] args) throws IOException, InvalidFormatException {
for (String arg : args) {
System.out.println("Arg " + arg);
}
//Other code removed
}
I really don't know what I am doing wrong when building jar file that it doesn't see Main class.

After I added env variable it start to see main, but now I am getting another crash:
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError:
org/apache/poi/openxml4j/exceptions/InvalidFormatException
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
at java.lang.Class.getMethod0(Class.java:3018)
at java.lang.Class.getMethod(Class.java:1784)
at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
Caused by: java.lang.ClassNotFoundException:
org.apache.poi.openxml4j.exceptions.InvalidFormatException
at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
It looks that java don't see library files in my jar file. If I check jar file with 7zip I can see library files inside of it.

Related

Runnable Jar file not detecting TestNG Classpath

I am trying to create a runnable jar file that has some TestNG tests.I can run the project files from Eclipse but when I create a jar file it can't find the required library. A possible classpath issue I guess.There are similar questions on the forum but none of them have any answers that could help me resolve this issue. When I run the jar file id gives the following error.
Exception in thread "main" java.lang.NoClassDefFoundError: org/testng/TestNG
at test.TestRunner.main(TestRunner.java:11)
Caused by: java.lang.ClassNotFoundException: org.testng.TestNG
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:606)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:168)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
... 1 more
This is my Class with the main method.
package test;
import org.testng.TestNG;
public class TestRunner {
static TestNG testNG;
public static void main(String[] args) {
testNG = new TestNG();
testNG.setTestClasses( new Class[] {App1.class});
testNG.run();
}
}
Then there is a class that has some very simple tests.Everything runs fine from eclipse but the jar file has the problem. The problem seems very simple but yet I have not been able to resolve it. I would appreciate any help. Thanks

openmpi + java, could not find or load main class

I am recently trying out the java binding of the openmpi library :
I have succesfully compiled the lib with the following configurations:
$/configure --prefix "/home/yuechuan/Application/.openmpi" --enable-mpi-java --with-jdk-dir="/usr/lib/jvm/java-8-oracle/" --with-jdk-header="/usr/lib/jvm/java-8-oracle/include/"
$sudo make install
I am able to compile a simple java program with : $mpijavac src/com/cyc115/pa2/Main.java
currently $echo $CLASSPATH gives /home/yuechuan/Github/parallel_pa2/src/com/cyc115/pa2/
However I am having trouble to get the class file to run. Here are some alternatives I have tried so far:
$java -Djava.library.path=/home/yuechuan/Application/.openmpi/lib/mpi.jar -cp .:/home/yuechuan/Application/.openmpi/lib/mpi.jar Main
which returns Error: Could not find or load main class Main error.
the pure and simple $java Main command returns NoClassDefFoundError
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: mpi/MPIException
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
at java.lang.Class.getMethod0(Class.java:3018)
at java.lang.Class.getMethod(Class.java:1784)
at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
Caused by: java.lang.ClassNotFoundException: mpi.MPIException
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 7 more
Here's the java file if anyone needs :
package com.cyc115.pa2;
import mpi.MPI;
import mpi.MPIException;
public class Main {
public static void main(String args[]) throws MPIException
{
MPI.Init(args);
int rank = MPI.COMM_WORLD.getRank();
int size = MPI.COMM_WORLD.getSize();
System.out.print("hello world");
MPI.Finalize();
}
}
Any ideas on how I can get the .class to run?
Since the package is :
package com.cyc115.pa2;
You need to put the file Main.java in a sub-directory named com/cyc115/pa2
The java library path should be a path to the directory containing libmpi_java.so rather than the path to mpi.jar.
To compile:
javac -cp /home/yuechuan/Application/.openmpi/lib/mpi.jar com/cyc115/pa2/Main.java
To run:
java -Djava.library.path=/home/yuechuan/Application/.openmpi/lib/ -cp .:/home/yuechuan/Application/.openmpi/lib/mpi.jar com.cyc115.pa2.Main
The following is not the problem, just an FYI:
It should be --with-jdk-headers with an s rather than --with-jdk-header however you do not have to specify it at all if you are already specifying --with-jdk-dir.

Docx4j Class Not Found Error

I am trying to run a tutorial from here Office Open XML. I have downloaded the Docx4j library and added it to the netbeans as a library.
the code snippet is given below
import org.docx4j.openpackaging.exceptions.Docx4JException;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
public class OfficeOpen {
public static void main(String[] args) throws Docx4JException {
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
wordMLPackage.getMainDocumentPart().addParagraphOfText("Hello Word!");
wordMLPackage.save(new java.io.File("src/main/files/HelloWord1.docx"));
}
}
when I run the above example I get the following error
Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
at org.docx4j.openpackaging.Base.<clinit>(Base.java:43)
at officeopen.OfficeOpen.main(OfficeOpen.java:24)
Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 2 more
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
looks like the JVM cannot find a class at run time, any suggestion. I am using Netbeans 8.0
You are missing some libraries that docx4j depends on. See http://htmlpreview.github.io/?https://github.com/plutext/docx4j/blob/master/docs/Docx4j_GettingStarted.html at docx4j dependencies.
Indeed you are missing slf4j that docx4j uses for for logging: http://www.docx4java.org/docx4j/docx4j-3_2_0/dependencies/slf4j-api-1.7.5.jar
Possibly you'll need many others.
I think this are all the dependencies, althought you may not need all of them: http://www.docx4java.org/docx4j/docx4j-3_2_0/dependencies/

NoClassDefFoundError exception

I got trouble in using my external jar file even if I put it inside class path correctly.
My external jar file is in the D: drive and my class path is "C:\Program Files\Java \jdk1.7.0_51\bin; D:\webcam-capture-0.3.10-RC6.jar; C:\Program Files\Java\jdk1.7.0_51\lib;" so please help me solve the problem. Below is my code:
import com.github.sarxos.webcam.Webcam;
import javax.swing.JOptionPane;
public class DetectWebcamExample {
public static void main(String[] args) {
try {
Webcam webcam = Webcam.getDefault();
if (webcam != null) {
System.out.println("Webcam: " + webcam.getName());
} else {
System.out.println("No webcam detected");
}
} catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}
}
}
the exeption detail is
Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
at com.github.sarxos.webcam.Webcam.<clinit>(Webcam.java:40)
at test.Test.main(Test.java:20)
Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 2 more
Java Result: 1
i amend the path with "C:\Program Files\Java\jdk1.7.0_51\bin;" and classpath "D:\webcam-capture-0.3.10-RC6.jar;." but the exception is still there
How can I solve this problem?
Java has found com.github.sarxos.webcam.Webcam so you have successfully added that JAR to the classpath.
What's happening now is that Webcam is trying to use a log4j class, and that's not on the classpath.
This is common - package A needs package B; package B needs package C; and you can spend a while finding JARs to fulfil chains of dependencies.
In this case you're going to need two log4j jars - log4j-api and one of a choice of log4j implementation jars. See the log4j web page for details.
If the hunt for dependency jars starts getting silly, consider using Maven or Ivy to handle your dependencies for you.

Could not find main class error when running a java wsdl client

I have written the following code, which is meant to be a client wsdl for http://www.nanonull.com/TimeService/TimeService.asmx?WSDL:
package time;
class Client {
public static void main(String args[]){
TimeService service = new TimeService();
TimeServiceSoap port= service.getTimeServiceSoap();
String result = port.getTimeZoneTime("UTC+10");
System.out.println("Time is "+result);
}
}
But when I try to run it with java I get the following:
C:\Program Files\Java\jdk1.6.0_22\bin>java client.Client
Exception in thread "main" java.lang.NoClassDefFoundError: client/Client
Caused by: java.lang.ClassNotFoundException: client.Client
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: client.Client. Program will exit.
Does this error mean I should be importing any classes?
The main class you wrote is time.Client, but you are trying to run client.Client. Better run it like this:
java time.Client
If this does not help, then you have a classpath problem - java cannot find the main class in the classpath. Set the classpath with -classpath option:
java -classpath classes-directory;list-of-jar-files time.Client
You need to set the classpath:
C:\Program Files\Java\jdk1.6.0_22\bin>java -cp . client.Client

Categories