Jar Can't Find Main Class - java

I get: Could not find the main class: org.dav.kin.Tester. Program will exit. when I attempt to run my jar file via java -jar tester.jar or java -classpath tester.jar org.dav.kin.Tester Does anyone know what is wrong and how to fix it? Below are additional details. Thanks.
Manifest File:
Manifest-Version: 1.0
Created-By: DKin
Class-Path: .
Main-Class: org.dav.kin.Tester
jar tf tester.jar
org/
org/dav/
org/dav/kin/
org/dav/kin/Tester.class
org/dav/kin/TesterCellRenderer.class
...
...
META-INF/
META-INF/MANIFEST.MF
UPDATE:
Jar file runs if I specify the system classpath, which contains the groovy-all-{version}.jar, like so: java -classpath tester.jar;"%CLASSPATH%" org.dav.kin.Tester Anyone know why I have to explicitly re-state the classpath (or more precisely, the groovy jar)?

Your jar file lacks a file with this name
/org/dav/kin/Tester.class
or you have special characters in your MANIFEST.MF file
MANIFEST.MF files have a particular syntax. It's best to use other tools to generate them; however some of the details I've encountered which increases the success of hand written files include:
Always make sure the lines are less than 72 characters long.
Always use \r\n (windows newline), even on non-windows systems.
Verify that all whitespace characters are spaces.
Verify that there are no nonprintable characters (htab, etc).
Sometimes a blank line at the end of the file helps.

Is Tester.class' package declaration org.dav.kin?

You have indicated that the you are using Groovy. Groovy does compile down to Java class files but it still requires the groovy runtime libraries. You need to make sure groovy is on the classpath as well as your classes. Try this:
java -classpath tester.jar;groovy-all-1.8.0.jar org.dav.kin.Tester

Just in case. I just solve exactly the same problem.
Instead of
Class-Path: .
in MANIFEST.MF
one should enumerate (with space) jars which are required in runtime, so it should be something like this:
Class-Path: groovy-all-2.4.5.jar relative/my-dependent-project-artifact.jar

Related

executable jar Could not find or load main class

I've been struggling with this common error and just can't resolve it. This application is composed of multiple packages and runs fine within JCreator (at the moment I need to use this IDE rather than Eclipse).
My manifest file is here (there are 2 blank lines at the end):
Manifest-Version: 1.0
Created-By: 1.6.0_45 (Sun Microsystems Inc.)
Main-Class: C:\COMPILE\MyProjects\douwe\classes\dykstra\dplus\main\DPMain
I wrote a bat file to create the jar:
jar -cvfm DPlus.jar C:\COMPILE\MyProjects\douwe\classes\MANIFEST.MF
C:\COMPILE\MyProjects\douwe\classes\dykstra\dplus\main*.class
C:\COMPILE\MyProjects\douwe\classes\dykstra\dplus\library*.class
C:\COMPILE\MyProjects\douwe\classes\dykstra\dplus\command*.class
C:\COMPILE\MyProjects\douwe\classes\dykstra\dplus\file*.class
C:\COMPILE\MyProjects\douwe\classes\dykstra\dplus\file\display*.class
C:\COMPILE\MyProjects\douwe\classes\dykstra\dplus\command*.class
C:\COMPILE\MyProjects\douwe\classes\dykstra\dplus\file*.class
C:\COMPILE\MyProjects\douwe\classes\dykstra\dplus\file\display*.class
C:\COMPILE\MyProjects\douwe\classes\dykstra\dplus\gui*.class
C:\COMPILE\MyProjects\douwe\classes\dykstra\dplus\gui*.class
C:\COMPILE\MyProjects\douwe\classes\dykstra\dplus\job*.class
C:\COMPILE\MyProjects\douwe\classes\dykstra\dplus\job*.class
C:\COMPILE\MyProjects\douwe\classes\dykstra\dplus\types*.class
C:\COMPILE\MyProjects\douwe\classes\dykstra\dplus\util*.class
When I try to execute with the command
C:\COMPILE\MyProjects\douwe\classes>java -jar DPlus.jar
I always get the error:
Error: Could not find or load main class C:\COMPILE\MyProjects\douwe\classes\dykstra\dplus\main\DPMain
Can anyone see what I'm doing wrong here?
Usually this error is due to MANIFEST.MF if the'res no application's entry point has been set.
Your manifest file should have this line of code
Main-Class: YourPackage.DPMain
Alternatively, you can do the following.
java -cp .;app.jar YourPackage.DPMain
In my implementation, there are something different from yours, you can refer:
(1) The folder(before compressed) structure
you need to add a META-INF folder and put your MANIFEST.MF in it
(2) The content in your MANIFEST.MF
I think your should use the package format instead of a folder tree format:
Manifest-Version: 1.0
Main-Class: com.loadtest.mgr.LoadTestStarter

Package does not exist error when package was added to classpath

Note, linked solutions (ex. Fatal Error: Unable to find package java.lang in classpath or bootclasspath) do not work.
I get this error, but the package is imported (commons... .jar)
org.apache.commons.lang3.tuple //does not exist import
org.apache.commons.lang3.tuple.MutableTriple
Source code
import org.apache.commons.lang3.tuple.MutableTriple;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.commons.lang3.tuple.Triple;
Build code:
export
JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home
/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/bin/javac
-target 1.8 -source 1.8 -classpath \ "../lib/commons-lang3-3.4.jar;../lib/httpclient-4.5.jar;../lib/httpcore-4.4.1.jar;../lib/org.json-20120521.jar;../lib/pdfbox-app-2.0.0-20150606.170503-1383.jar;../src/:../lib/commons-lang3-3.4-javadoc.jar;../lib/pdfbox-app-2.0.0-20150606.170503-1383-sources.jar" \ -d output \ ../src/com/tymaf/pdf/*.java
How to fix this problem?
Double check your classpath. Looks like you mixed delimiters ; and :.
Also instead of including jar with compiled classes (library itself). You've included java-docs and sources that are useless in classpath.
../src/:
../lib/commons-lang3-3.4-javadoc.jar;
../lib/pdfbox-app-2.0.0-20150606.170503-1383-sources.jar
Here is my suggestion
How to compile and use .jar extension
.jar extension can be imported different ways depending on your environment and IDE.
Here how it work as native mode from console.
Download the .jar.zip library from
http://www.java2s.com/Code/Jar/c/Downloadcommonslang333jar.htm
Create a folder in your working (project) directory call it libs
Unzip the downloaded file and copy commons-lang3-3.3.jar to your working directory libs
I have also created a class just for testing call it TheNewWork.java and added the 3 imports.
Now from your working directory c:\projects for Compile:
javac -classpath "/Projects/libs/commons-lang3-3.3.jar;" TheNewWork.java
And for running it:
java -classpath "/Projects/libs/commons-lang3-3.3.jar;" TheNewWork
If you have more than one .jar just add ; for Windows and : for Linux. Btw I use windows 10 cmder console and java jdk1.8.0_66. In other OS console you might need to put .:Projects...etc in stead of /Projects...etc. but the idea is the same.
UPDATE
In windows it is possible to set classpath like
set CLASSPATH=%CLASSPATH%;C:\Projects\libs\commons-lang3-3.3.jar
OR in Linux
export CLASSPATH=".:/Projects/libs/commons-lang3-3.3.jar"
Then you can run javac TheNewWork.java but it is personal taste to do it this or the other way. Some things similar is also possible to do in other OS.
Last thing, if you lazy and do neither want to write a full command line nor create a classpath, you could create a batch file with the full command line and run it that way in stead ;)
Some references:
http://docs.oracle.com/javase/8/docs/technotes/tools/windows/classpath.html
https://www3.ntu.edu.sg/home/ehchua/programming/howto/JDK_Howto.html
https://www.chilkatsoft.com/java-classpath-Windows.asp
I hope this solves your problem
Before the solution
After the solution
NOTE
In addition thanks to #MarkPeters notified me on my previous answer: Adding application dependencies directly to the JRE libs is not a good approach, as it makes the JRE suitable for running only one Java application, rather than being a generic runtime. Plus it would complicate whatever deployment the OP wants to do. lib/ext is made for extending the core Java APIs, as described here: docs.oracle.com/javase/tutorial/ext/basics/install.html. Not for normal application dependencies.

Execute JAR from lib directory

I have the "SomeJarA.jar" with the followiung structure:
SomeJarA
|
|--lib/SomeJarB.jar
|--com/.../SomeClass.class
Im trying to execute the "SomeJarB.jar" in "SomeClass.java" using:
Runtime.getRuntime().exec("java -jar SomeJarB.jar");
Is this possible?
As far as I know, if there's a way to achieve this, it is going to be a tricky and highly unsupported arrangement. I'll look around for some supporting documentation, but I'm inclined to say "No."
This related link might help, though.
EDIT: Found the reference I wanted. It's here. Specifically, there's a note which says, "The Class-Path header points to classes or JAR files on the local network, not JAR files within the JAR file or classes accessible over internet protocols."
This is from user #Tezar and Question Classpath including JAR within a JAR
You can use "Class-Path" in your manifest file.
For example:
Create manifest file MANIFEST.MF
Manifest-Version: 1.0
Created-By: Bundle
Class-Path: ./custom_lib.jar
Main-Class: YourMainClass
Compile all your classes and run
jar cfm Testing.jar MANIFEST.MF *.class custom_lib.jar
c stands for create archive f indicates that you want to specify file v
is for verbose input m means that we will pass custom manifest file
Be sure that you included lib in jar package. You should be able to run
jar in the normal way.
based on: http://www.ibm.com/developerworks/library/j-5things6/
See the linked Question. It seems like the sanest method for jars in jars and avoids third-party class loaders. If you feel like upvoting, go to Tezar's answer and upvote that entry, not this one, as this is just a copy.

javac compiler error for a source file that is not provided

I'm getting this error when I run javac -d classes myfiles.... The strange thing is, I do not have PooledPreparedStatementHandler.java in any folder nor do I have the PooledPreparedStatementHandler sub-string in any of my source files. The only reference I found was PooledPreparedStatementHandler.class in the jaybird jar (used and needed in my classpath).
./lib/jaybird-full-2.1.6.jar (all class files, no java files)
Here is the error:
org/firebirdsql/pool/PooledPreparedStatementHandler.java(org/firebirdsql/pool:PooledPreparedStatementHandler.java):32: package org.apache.xalan.lib.sql does not exist
import org.apache.xalan.lib.sql.XConnection;
This is the compile command:
find src/main/java -type f -name "*.java"| xargs javac -target 1.5 -d war/WEB-INF/classes
What is the source of this error? I'm using oracle javac 1.6.0_26
From the source code, it looks like PooledPreparedStatementHandler tries to import org.apache.xalan.lib.sql.XConnection, so you're dependent on it. And as you don't have that on your build classpath, you're getting the error.
It looks like you have a modified copy of the Jaybird sources inside your project that is compiled together with the rest of your code. The original Jaybird 2.1.6 version of org.firebirdsql.pool.PooledPreparedStatementHandler (links to the 2.1.6 tag on GitHub) does not reference org.apache.xalan.lib.sql.XConnection.
It actually looks like it is trying to compile a version of PooledPreparedStatementHandler.java, and not just use the class from the Jaybird jar file. You might want to check if the result of find src/main/java -type f -name "*.java" includes any Jaybird .java files (ie in the org.firebirdsql.* packages).
You could also try to include Xalan-J on your classpath while building. If a copy of PooledPreparedStatementHandler gets included in the destination folder, then the source file is somehow getting included in your build.
Mark actually gave me the answer in a comment. If you want the points Mark, move it to a answer. Until then, JayBird lists Xalan 2.3.1 as a dependence. I just needed to include that in the classpath.

Specifying classpath for a jar

I am trying to configure the classpath of a JAR so that my ResourceBundle can pick up property files from it.
If I run it from the .class files and specify the -cp flag it works fine, and System.err.println(System.getProperty("java.class.path")); will print the path specified in the -cp flag.
If I try and create a jar file for it, System.err.println(System.getProperty("java.class.path")); always prints the path of the jar file, and the property files aren't picked up.
It seems if you are running it as a jar file you can't specify the -cp flag (which was what I was hoping, as it's common to switch which property files are being used). I've tried specifying it in the jar manifest instead, but it's still not working.
Here is the code and manifest from a test jar that doesn't seem to work:
public final class Test {
public static void main(final String[] args) {
System.err.println(System.getProperty("java.class.path"));
}
}
Manifest-Version: 1.0
Created-By: 1.6.0_20 (Sun Microsystems Inc.)
Main-Class: Test
Class-Path: /home/ajanuary/Projects/test/
edit
The original path was rather meaningless so I changed it. I want to point to a directory which the ResourceBundle can find the property files in.
If you use -jar, -cp is ignored:
-jar
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.
Source: java - the Java application launcher
I would instead read a property in my Java application (that property could indicate from where resources should be loaded).
Example of how to execute the application would then be:
java -Dkey=value -jar application.jar
You can't use classpath wildcards in the manifest.
Take a look at Setting the classpath for more information on how classpath works:
class path wildcards are not honored
in the Class-Path jar-manifest header.
Yes, and for the shell, ~ means $HOME, but for java, it doesn't mean anything.
The problem was that I also had an index file. If you have an index file, the Class-Path will be ignored.

Categories

Resources