execute jar error could not find or load main class - java

sorry for another "could not find or load main class" question.
I'm new to Java. I searched for a while but couldn't find the reason.
My project is a very simple one created with maven.
mvn archetype:generate -DgroupId=mycompany -DartifactId=faker -DinteractiveMode=false
pom.xml(default, not change)
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>mycompany</groupId>
<artifactId>faker</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>faker</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
and source code(default, not change)
package mycompany;
public class App {
public static void main( String[] args ) {
System.out.println( "Hello World!" );
}
}
I build jar file with jar command.
~$ jar cvfm MyCompany.jar manifest.txt src/main/java/mycompany/App.java
# added manifest
# adding: src/main/java/mycompany/App.java(in = 705) (out= 352)(deflated 50%)
manifest.txt
~$ cat manifest.txt
Main-Class: mycompany.App
# (has an empty new line here)
but whenever I executed it, it give me following error:
~$ java -jar MyCompany.jar
Error: Could not find or load main class mycompany.App
Where is wrong ?
openjdk version "1.8.0_181"
Apache Maven 3.5.2
Thanks #ohlec and #Kayaman, to be more specific, here's the way to solve it.
# get App.class
~$ cd src/main/java/mycompany/ && javac App.java
# back to faker folder
~$ jar cvfm MyCompany.jar manifest.txt -C src/main/java mycompany/App.class
~$ java -jar MyCompany.jar
# Hello World!

Your jar should have a directory structure that mirrors the package structure; in your case, src/main/java is included, although that is not a package prefix. Use -C to specify the root directory:
jar cvfm MyCompany.jar manifest.txt -C target mycompany/App.class
Edit: Also, as Kayaman pointed out, you need to include the .class file, not the .java file.

Related

Exception in thread "main" java.lang.UnsatisfiedLinkError: Cannot find TensorFlow native library for OS: linux, architecture: x86_64

I am trying setup TensorFlow Java application in Eclipse Oxygen (OS: Ubuntu Linux 16.x). I installed Tensorflow and followed the process mentioned in official documentation for Java (Maven Project) installation. I downloaded libtensorflow-1.3.0.jar, jni files and included in the build path. When I execute the program I get this following error
Exception in thread "main" java.lang.UnsatisfiedLinkError: Cannot find TensorFlow native library for OS: linux, architecture: x86_64. See https://github.com/tensorflow/tensorflow/tree/master/tensorflow/java/README.md for possible solutions (such as building the library from source). Additional information on attempts to find the native library can be obtained by adding org.tensorflow.NativeLibrary.DEBUG=1 to the system properties of the JVM.
at org.tensorflow.NativeLibrary.load(NativeLibrary.java:66)
at org.tensorflow.TensorFlow.init(TensorFlow.java:36)
at org.tensorflow.TensorFlow.<clinit>(TensorFlow.java:40)
at com.tensorflow.malwaredetection.App.main(App.java:13)
App.java
package com.tensorflow.malwaredetection;
import org.tensorflow.TensorFlow;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
System.out.println( "Hello World!"+ TensorFlow.version() );
}
}
POM.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>TensorFlow</groupId>
<artifactId>MalwareDetection</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>MalwareDetection</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<exec.mainClass>App</exec.mainClass>
<!-- The sample code requires at least JDK 1.7. -->
<!-- The maven compiler plugin defaults to a lower version -->
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.tensorflow</groupId>
<artifactId>libtensorflow</artifactId>
<version>1.3.0</version>
</dependency>
</dependencies>
</project>
I got tired of this error and tried to do this in an old-fashioned way. Created App.java in a separate folder and included jar, jni files in the same directory. When I execute this from command line, I get different error
dev#ubuntu:~/Downloads$ javac -cp libtensorflow-1.3.0.jar Test1.java
dev#ubuntu:~/Downloads$ java -cp libtensorflow-1.3.0.jar:. -Djava.library.path=./jni Test1
Error: Could not find or load main class Test1
dev#ubuntu:~/Downloads$
I think you need to include jni library dependency in your pom.
<dependency>
<groupId>org.tensorflow</groupId>
<artifactId>libtensorflow_jni</artifactId>
<version>1.1.0</version>
</dependency>
When you have tensor flow dependency in your pom.xml file as below
<dependencies>
<dependency>
<groupId>org.tensorflow</groupId>
<artifactId>tensorflow</artifactId>
<version>1.14.0</version>
</dependency>
It will download required 3 libraries
tensorflow-1.14.0.jar
libtensorflow-1.14.0.jar
libtensorflow_jni-1.14.0.jar
So you have to manually extract the 3rd jar(libtensorflow_jni) and get your OS compatible file like for windows you have to copy the tensorflow_jni.dll file and paste in your project root directory
that will solve your problem.
For Linking error the JNI library which you have downloaded is
not compatible with OS you are running,
is not in PATH () or
with lesser probability TensorFlow bu which you should be able to confirm from TF triage.
For second could not find class
your PATH must be appended with "." so that it can find class in current folder from where it is running
Less probability is if class name/package is not same.
If you can post code for class will check

Trouble building Vaadin application with Maven in IntelliJ

I am looking to start a project using Vaadin and I want to use Maven as the repository manager, but I am having a lot of trouble starting the Maven project.
I tried creating a project in IntelliJ using the vaadin-archetype-application-example and it doesn't create any directory.
I also tried creating the project with the mvn commands and I found a lot of troubles too. When I create the Maven project this error is shown in the log:
-Dmaven.multiModuleProjectDirectory system propery is not set. Check $M2_HOME environment variable and mvn script match.
[ERROR] Maven execution terminated abnormally (exit code 1)
How can I deploy a Maven project with IntelliJ? I have the Maven plugin installed and the mvn command seems to work just fine.
This is my pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.imtoolazytoadmin</groupId>
<artifactId>ImTooLazyToAdmin-master</artifactId>
<version>1.0-SNAPSHOT</version>
</project>
Use this command to create your project and then import it to your IDE.
$ mvn archetype:generate \
-DarchetypeGroupId=com.vaadin \
-DarchetypeArtifactId=vaadin-archetype-application \
-DarchetypeVersion=7.5.9 \
-DgroupId=your.company \
-DartifactId=project-name \
-Dversion=1.0 \
-Dpackaging=war
More info about creating Vaadin projects in IntelliJ here.

maven external jar dependency

I am trying to use a jar given by my teacher in my code. So I placed the jar in a lib directory in my project and ran this command :
mvn install:install-file -Dfile=lib/IDLogger.jar -DgroupId=IDLogger -DartifactId=IDLogger -Dversion=1.0 -Dpackaging=jar
Everything is fine.
Then I add the dependency to the pom :
<dependency>
<groupId>IDLogger</groupId>
<artifactId>IDLogger</artifactId>
<version>1.0</version>
<scope>compile</scope>
</dependency>
But then when I try to use the jar, the compilation fails and tells me that the symbol(the class) can't be found. I even tried adding an import statement :
import IDLogger.IDLogger;
but it tells me that there is no such package.
How can I use this jar in my code in maven?
This is the code :
IDLogger logger = IDLogger.getInstance();
...
logger.logID(id);
I get the symbol IDLogger not found error
This is as much as I know about this custom jar...
Try to add your lib in system scope :
1. add in your project :
Your Project/lib
+IDLogger.jar
2. in your pom :
<dependency>
<groupId>IDLogger</groupId>
<artifactId>IDLogger</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/IDLogger.jar</systemPath>
</dependency>

could not find or load main class org.tesng.TestNG

I am using testng-6.8.21.jar for writing test case from the following link:
http://www.tutorialspoint.com/testng/testng_tutorial.pdf
I am able to compile the java file TestNGSimpleTest.java
but when I try to use this command:
java -cp "C:\TestNG_WORKSPACE" org.testng.TestNG testng.xml
it says:
could not find or load main class org.tesng.TestNG
You must provide full path to the jars in the classpath. For example :
java -cp '/path/to/testng-6.8.8.jar' org.testng.TestNG testng.xml
But testng requires other dependencies that you also must include in the classpath :
\- org.testng:testng:jar:6.1.1:test
+- junit:junit:jar:3.8.1:test
+- org.beanshell:bsh:jar:2.0b4:test
+- com.beust:jcommander:jar:1.12:test
\- org.yaml:snakeyaml:jar:1.6:test
The easiest way is to use a dependency manager. For example, Maven.
Briefly, you need (not required but it makes everything easier) to have a standard project structure :
main-directory
pom.xml <- File required by maven. It always has this name.
-src
-main
-java <- Place your Java classes here
-resources <- Place your images, conf files here etc.
-test
-java <- Place your java test classes here
-resources <- Place your test resources here.
Then, with this simple pom.xml, Maven understand that you want testng and downloads testNG's dependencies :
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany</groupId>
<artifactId>my-app-name</artifactId>
<version>1.0.0-SNAPSHOT</version>
<!-- Declare your dependencies here-->
<dependencies>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.1.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Then launch :
mvn test
And if you want to have a view on the dependencies, use :
mvn dependency:tree
(This is how I got the preceding dependency tree)

Out-of-tree build with maven. Is it possible?

I start learning packaging for several distros (currently Cygwin and Debian).
They have requirement to build system to allow out-of-tree build (synonym out-of-source build):
http://wiki.debian.org/UpstreamGuide#Out-of-Tree_Builds
To work-around "dumb" build system for example cygport recommend use lndir (from xutils project):
lndir ${S} ${B}
cd {B}
...build-commands...
I read mvn(1) man page but doesn't found anything appropriated. Next I just try:
$ mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
...
$ pwd
/maven/simple
$ ls
my-app
$ mvn -f my-app/pom.xml compile
...
[INFO] --- maven-compiler-plugin:2.0.2:compile (default-compile) # my-app ---
[INFO] Compiling 1 source file to /maven/simple/my-app/target/classes
As you can see target directory created in source root hierarchy while I look for a way to avoid this.
Is it possible out-of-tree build with maven? And how?
You could do like this to get it in your current working directory:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.stackoverflow</groupId>
<artifactId>Q13173063</artifactId>
<version>1.0-SNAPSHOT</version>
<name>${project.artifactId}-${project.version}</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<buildDir>${user.dir}</buildDir>
</properties>
<build>
<directory>${buildDir}</directory>
</build>
</project>
Then you can issue
mvn -f my-app/pom.xml compile
And it will give you your classes in the current working directory.
And easily change to another output directory:
mvn -f my-app/pom.xml -DbuildDir=/tmp/build compile
It might be as simple as having a
<build>
<directory>/your/build/directory</directory>
</build>
in your pom.xml. /your/build/directory need not be in the source tree and can be parameterized using the usual ${...} syntax.
Cheers,

Categories