I have an application, which I try to build using Maven. I can build a jar, but running it does nothing: it can't find a Main class. I set however the MainClass in the POM. When I try to run the MainPane.class in the target folder from the command line returns:
"Error: could not find or load Main class MainPane". (After navigating inside the target folder and run 'java MainPane')
I can run this class from Eclipse and it has a main method:
public static void main(String[] args) {
System.out.println("test");
}
I should be able to run the class file in the target folder right? What can possibly go wrong?
You need to tell java where the class is by defining the -classpath parameter
java -classpath classes MainPane
Or from the project directory
java -classpath target/classes MainPane
Also make sure you are using the full package name if you have one
java -classpath target/classes my.package.name.MainPane
#AndyGeeDe
TomEE treibt Tomitribe! | http://tomee.apache.org
java -jar myjar.jar
Will only work if the jar's manifest contains the "Main-Class: classname" entry. Check for that. If it is not present then you can still run you application via this command:
java -cp ./my.jar package/class_name
Try googling on "maven executable jar". Once you get the build correct, you can run it using
java -jar myjar.jar
Java is not terribly good at this. The executable jar needs all the dependent jars unpacked, and a manifest file included that names the main class. To my mind is less than elegant but it does work.
Related
I have a very simple piece of code I am trying to run on the Windows command line (Windows 7). It runs in Eclipse fine.
I have read How to make javac find JAR files? (Eclipse can see them)
and
https://docs.oracle.com/javase/1.5.0/docs/tooldocs/windows/classpath.html#Understanding
but clearly am missing something or misunderstanding something.
Here's the code:
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.common.notify.Notifier;
public class MakeUniqIDs {
public static void main(String[] args) {
for (int i=0; i<10; i++){
System.out.println(EcoreUtil.generateUUID());
}
}
}
When I try to compile it with javac I get following error message: "MakeUniqIDs.java:1: error: package org.eclipse.emf.ecore.util does not exist"
I am in the src directory where the above code lives, and used the following to attempt to compile it:
javac -classpath "..\lib\org.eclipse.emf.ecore_2.13.0v28170609-0707.jar" MakeUniqIDs.java
I put the jar files in the lib directory, and also tried putting the path to the eclipse plugins directory into the classpath, but still no go.
You have to specify external JAR on the classpath.
java -cp path/some.jar; etc.
You are using classes that are part of Eclipse itself. You could dig out their JAR, but that's generally not a good idea. generateUUID() seems to be used to create a UUID in source file based on the file content, which is an IDE feature.
First ensure that the jar is in that path, and the name is exactly the same ls ..\lib or dir ..\lib. Then use the command (with the right path):
javac -classpath "..\lib\org.eclipse.emf.ecore_2.13.0v28170609-0707.jar" MakeUniqIDs.java
Also note that you have imported org.eclipse.emf.common.notify.Notifier;, and that class is in the jar org.eclipse.emf.common not ecore, you can remove the line (since you are not using it) or add the jar separated with ;.
Example:
javac -classpath "..\lib\org.eclipse.emf.ecore_2.13.0v28170609-0707.jar;..\lib\org.eclipse.emf.common_2.13.0v28170609-0707.jar" MakeUniqIDs.java
I am having trouble running a JAR file from terminal which has both native and .jar dependencies. Okay, my goal isn't to run it from the terminal, but to run it as a separate process with Java's Runtime.getRuntime().exec function, but if I can't run it from the terminal, then I also can't run it via. The JAR file I am trying to run depends on a number of other jar files as well as a number of .so libraries. I'm trying to put put all the .jar dependencies and .so dependencies in their own folders and then run the jar file with:
java -cp /home/johnmichaelreed/Desktop/Dropbox/Libjitsi_linux_64/* -Djava.library.path=/home/johnmichaelreed/Desktop/Dropbox/Libjitsi_linux_64/lib/native/linux-64/* -jar /home/johnmichaelreed/NetBeansProjects/SendReceive/dist/SendReceive.jar
Where "/home/johnmichaelreed/Desktop/Dropbox/Libjitsi_linux_64/" contains all the JAR files and "/home/johnmichaelreed/Desktop/Dropbox/Libjitsi_linux_64/lib/native/linux-64/" contains all the .so files and the main JAR file to run is "/home/johnmichaelreed/NetBeansProjects/SendReceive/dist/SendReceive.jar", but I keep getting this error:
Error: Could not find or load main class
.home.johnmichaelreed.Desktop.Dropbox.Libjitsi_linux_64.some-compressed-jar-file.jar
Where some-compressed-jar-file.jar is one of the .jar files that my application is supposed to use.
Here's my Java JAR dependencies folder:
And here's my native libraries dependencies folder:
UPDATE:
Okay, this is the solution:
java -Djava.library.path=/home/johnmichaelreed/Desktop/Dropbox/Libjitsi_linux_64/lib/native/linux-64 -cp '/home/johnmichaelreed/NetBeansProjects/SendReceive/dist/SendReceive.jar:/home/johnmichaelreed/Desktop/Dropbox/Libjitsi_linux_64/*' Main
With attempt at command line args:
java -Djava.library.path=/home/johnmichaelreed/Desktop/Dropbox/Libjitsi_linux_64/lib/native/linux-64 -cp '/home/johnmichaelreed/NetBeansProjects/SendReceive/dist/SendReceive.jar:/home/johnmichaelreed/Desktop/Dropbox/Libjitsi_linux_64/*' Main "arg"
You can't use -jar and -cp at the same time.
What you can do, is adding your jar to the classpath and then specify your Main class to run. You can also specify the jar dependencies within the manifest of your jar.
Please have a look here for more details.
Assuming your Main class is in called Main and in the package foo.bar, then a possible call may look like:
java -cp "/home/johnmichaelreed/Desktop/Dropbox/Libjitsi_linux_64/*;/home/johnmichaelreed/NetBeansProjects/SendReceive/dist/SendReceive.jar" -Djava.library.path="/home/johnmichaelreed/Desktop/Dropbox/Libjitsi_linux_64/lib/native/linux-64/*" foo.bar.Main
Is there a way to pass an external jar file when running a .jar application?
I'm trying to run my jar like this:
java -jar myJar.jar -cp externalJar.jar
The jar file executes fine but I want to look for classes in the external file. I can't include the other classes into my jar, because I want to be able to put any jar file in the same folder as my Jar file and look for classes in there.
The only way to do this right now is by running my app like this:
java -cp myJar.jar;externalJar.jar MainClass
I do not want to explicitly enter the path to my MainClass to run it's main method.
It really seems that the -cp option is completely ignored when you use the -jar option. At least this is what you can read on the manpage of java about the -jar option:
Execute a program encapsulated in a JAR file. The first argument is
the name of a JAR file instead of a startup class name. In order for
this option to work, the manifest of the JAR file must contain a line
of the form Main-Class: classname. Here, classname identifies the
class having the public static void main(String[] args) method that
serves as your application's starting point. See the Jar tool
reference page and the Jar trail of the Java Tutorial for information
about working with Jar files and Jar-file manifests.
When you use this option, the JAR file is the source of all user classes, and other user
class path settings are ignored.
Note that JAR files that can be run with the "java -jar" option can
have their execute permissions set so they can be run without using
"java -jar". Refer to Java Archive (JAR) Files.
I found this in this blogpost here: http://happygiraffe.net/blog/2009/04/30/java-jar-blats-your-classpath/
Did you try adding a specific folder to the classpath during startup and then add your jar file to the folder at later point ?
I have a single .java (driver.java) file I'm trying to compile and run from the command-line. It uses the external library called EXT.jar, whose structure is just a folder called EXT with a few dozen classes within it.
So I run:
javac -cp EXT.jar driver.java
This compiles the class just fine.
then when I run:
java -cp EXT.jar driver
I get a java.lang.NoClassDefFoundError.
Oddly enough, if I unpack the JAR (so now I have a folder in the root directory called EXT), the last command works just fine!! Driver will execute!
Is there any way I can make the driver.class look for the need class files from EXT.jar/EXT/*class instead of an actual EXT folder?
Thanks!
You're compiling the class to the local directory. So when you run it, you need to include the current directory in your classpath. E.g.:
java -cp .;EXT.jar driver
Or in linux:
java -cp .:EXT.jar driver
With the way you have it now, you're saying your classpath is only EXT.jar (along with whatever is in the CLASSPATH environment variable) and nothing else (which is why the current directory, where driver.class is located, is excluded)
in the java test I have :
package Tester.GUI.api
public class Test1{-----}
in the ".bat" :
<path to java> -classpath<all jar defined in the classpath separated by ";"> org.junit.runner.JUnitCore Tester.GUI.api.Test1
when I launch th ".bat" I have the following :
JUnit version 4.6
Could not find class: Tester.GUI.api.Test1
Time: 0,203
OK (0 tests)
I have verified jar files , typo but not found the cause
someone could help please?
The -classpath option needs to take a set of directies and/or jar files. Currently those are missing, so your main class (JUnitCore) is being used as the classpath, and Test1 used as the main class. I'm guessing you want JUnitCore as the main class and Test1 as the argument.
Try something like this (substituting the actual names of the jars you're using for jar-file1, jar-file2):
java -classpath <jar-file1.jar>;<jar-file2.jar> org.junit.runner.JUnitCore Tester.GUI.api.Test1
Edit:
Assuming jar files are specified correctly, the error message indicates that the Test1 class isn't on the classpath. If you're using an IDE, you need to find the output directory for compilation (for example, in Eclipse it is <project>/bin by default). Within this directory you will find other directories with the structure Tester/GUI/api. A file called Test1.class will be in the api directory. It is this output directory that needs to be added to the classpath (that is, the one above the Tester/GUI/api structure).
It says that the class that it cannot find is Tester.GUI.api.Test1. What directory are you running the test from, and what directory is your Test1 code in?
If you set the -classpath variable, you may also need to include the current directory in your classpath. So, if your original command was
java -classpath C:\foo.jar;D:\bar1\bar2.jar org.junit.runner.JUnitCore Tester.GUI.api.Test1
you could add the current directory to your classpath by adding ;. to the end of your -classpath option like this:
java -classpath C:\foo.jar;D:\bar1\bar2.jar;. org.junit.runner.JUnitCore Tester.GUI.api.Test1
This will work as long as the Test1 class is in the Tester\GUI\api directory under the current directory.
The tricky thing with setting the classpath in Java is that you don't want to give the directory where your actual .java files are, but instead you want to give the directory under which you can find the directories that are named for the packages in your code.
For example, if I compiled a class Foo in a package bar.baz, then I should have a file named Foo.class in a directory named baz inside another directory named bar. If I want to include Foo in my classpath, and if Foo.class is located at
C:\Users\Joe\Code\bar\baz\Foo.class, then I have to either run
java -classpath C:\Users\Joe\Code [main class goes here]
or else I have to change my directory to C:\Users\Joe\Code and run
java -classpath . [main class goes here]