I'm trying to execute jar file of netbeans swing small project.
The file isn't executing.
I need to make it work by double clicking on the jar file that is located in:
C:\Users\armyTik\Documents\NetBeansProjects\Notepad\dist\Notepad.jar
extra details:
properties -> Build -> Packaging -> "Build JAR after compiling" and "Copy Dependent Libraries" are checked.
properties -> run -> Main class: "notepad.NotepadComponent"
In the cmd, by manually executing the jar file I get the following error:
cd C:\Users\armyTik\Documents\NetBeansProjects\Notepad\dist
java -jar Notepad.jar
"Error: Could not find or load main class notepad.Notepad"
What should I do, specifically?
EDIT:
I extracted the jar file and got:
META-INF/MANIFEST.MF
notepad/FileManager.class
notepad/icon.gif
notepad/NotepadComponent$1.class
notepad/NotepadComponent$2.class
notepad/NotepadComponent$3.class
notepad/NotepadComponent$4.class
notepad/NotepadComponent$5.class
notepad/NotepadComponent$6.class
notepad/NotepadComponent$7.class
notepad/NotepadComponent.class
notepad/Thumbs.db
META-INF/MANIFEST.MF content:
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.9.4
Created-By: 1.8.0_102-b14 (Oracle Corporation)
Class-Path:
X-COMMENT: Main-Class will be added automatically by build
Main-Class: notepad.NotepadComponent
Update2:
I got new exception while trying to open the jar from the cmd:
While opening the project from netbeans, the icon is shown with no exception.
*****When I remove the icons , The jar is working! but I need to keep the icons in the application.
This is because your classpath is not correct.
Try it like this:
java -cp "<PATH>/Notepad.jar" notepad.Notepad
If this does not work then unpack the jar file and confirm the package and class name is correct, Print CLASSPATH env variable or check java and javac versions.
Related
I have jar file with manifest in it
Manifest-Version: 1.0
Build-Jdk: 1.7.0_67
Created-By: Apache Maven 3.2.3
Main-Class: com.company.main.Main
Archiver-Version: Plexus Archiver
And the jar has compile dependency to external library
compile 'org.apache.commons:commons-lang3:3.3.2'
So I want to execute it comandLine I wrote:
java -cp C:\commons-lang3-3.3.2.jar -jar myJar-1.0.0.jar
But
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/lang3/StringUtils
at ...
How to add this file on the class path?
PS. If I use the "bad way" and copy-paste commons-lang3-3.3.2.jar in the ...jre7\lib\ext folder. Everything is working.
If you're not using the -jar option then you need to specify the main class to run, as the manifest will not be interrogated:
java -cp C:\commons-lang3-3.3.2.jar;.\myJar-1.0.0.jar com.company.main.Main
The classpath (-cp) option is ignored if using the -jar option (in that case the manifest should reference any other required jars via its classpath directive).
"Bad Way"..? You should never include your jar files inside the java directories.. How do you expect the users of your application to use your jar when they are using the standard java..?
either you can use the command suggested by #tombola82 or you can include the commons-lang jar in your project itself so that you can refer it.
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
I need help including imported jar files into my java program in Linux. Here is the program:
import java.sql.*;
public class CreateCoffees
{
public static void main(String args[])
{
try {
Class.forName("com.ibm.db2.jcc.DB2Driver");
}
catch(java.lang.ClassNotFoundException e) {
System.err.print("ClassNotFoundException: ");
System.err.println(e.getMessage());
System.exit(1);
}
}
}
In order to execute Class.forName("com.ibm.db2.jcc.DB2Driver"); I need two .jar files added into the classpath:
db2jcc_license_cu.jar
db2jcc4.jar
I put these jar files into the same directory as my CreateCoffees.java file, then compile and run it like this:
javac CreateCoffees.java
java CreateCoffees
But I got this error
ClassNotFoundException: com.ibm.db2.jcc.DB2Driver
Then I tried the "-classpath" option
javac -classpath ./db2jcc_license_cu.jar:./db2jcc4.jar CreateCoffees.java
java -classpath ./db2jcc_license_cu.jar:./db2jcc4.jar CreateCoffees
but got this
Exception in thread "main" java.lang.NoClassDefFoundError: CreateCoffees
Caused by: java.lang.ClassNotFoundException: CreateCoffees
How to I include those jar files into a my runnable jar so I can run it with java -jar myjar.jar ?
Try this
java -classpath ./db2jcc_license_cu.jar:./db2jcc4.jar:. CreateCoffees
when you use -classpath it looses current directory from classpath so it needs . in classpath as well explicitly
How to include the jars of your project into your runnable jar:
I'll walk you through it step by step with Eclipse Version: 3.7.2 running on Ubuntu 12.10. I'll also show you how to make the build.xml so you can do the ant jar from command line and create your jar with other imported jars extracted into it.
Basically you ask Eclipse to construct the build.xml that imports your libraries into your jar for you.
Fire up Eclipse and make a new Java project, make a new package 'mypackage', add your main class: Runner Put this code in there.
Now include the mysql-connector-java-5.1.28-bin.jar from Oracle which enables us to write Java to connect to the MySQL database. Do this by right clicking the project -> properties -> java build path -> Add External Jar -> pick mysql-connector-java-5.1.28-bin.jar.
Run the program within eclipse, it should run, and tell you that the username/password is invalid which means Eclipse is properly configured with the jar.
In Eclipse go to File -> Export -> Java -> Runnable Jar File. You will see this dialog:
Make sure to set up the 'save as ant script' checkbox. That is what makes it so you can use the commandline to do an ant jar later.
Then go to the terminal and look at the ant script:
So you see, I ran the jar and it didn't error out because it found the included mysql-connector-java-5.1.28-bin.jar embedded inside Hello.jar.
Look inside Hello.jar: vi Hello.jar and you will see many references to com/mysql/jdbc/stuff.class
To do ant jar on the commandline to do all this automatically: Rename buildant.xml to build.xml, and change the target name from create_run_jar to jar.
Then, from within MyProject you type ant jar and boom. You've got your jar inside MyProject. And you can invoke it using java -jar Hello.jar and it all works.
I am struggling to get my Java program to run on AIX. I used Eclipse on Windows to create a runnable Jar file, jRams.jar below. I kept on getting a class not found error, until finally I put all the external libraries in the same directory.
$ ls
JAXB2_20081030.jar
JAXB2_20110601.jar
activation.jar
asjava.jar
commons-beanutils-1.8.3.jar
commons-beanutils-bean-collections-1.8.3.jar
commons-beanutils-core-1.8.3.jar
commons-codec-1.5.jar
commons-collections-3.2.1.jar
commons-configuration-1.6.jar
commons-digester-2.1.jar
commons-jxpath-1.3.jar
commons-lang-2.6.jar
commons-logging-1.1.1.jar
commons-logging-adapters-1.1.1.jar
commons-logging-api-1.1.1.jar
jRams.jar
jaxb-api.jar
jaxb-impl.jar
jaxb-xjc.jar
jaxb1-impl.jar
jremote.jar
jsr173_1.0_api.jar
log4j-1.2.16.jar
netty-3.2.4.Final.jar
$
Still, I get the class not found error.
$ java -jar jRams.jar
The java class is not found: com.jbase.jremote.JRemoteException
jremote.jar definitely contains JRemoteException. Why isn't this working?
UPDATE
Thank you for your straight-to-the-point answers. I now understand the nature of a java application and a manifest file far better.
Turns out my ftp client was transferring in ASCII mode and not Binary, so the jar files were corrupt. I have learned a great deal, nonetheless.
When using the -jar option, you need to specify which jar-files should be on your class path in the manifest file. Just having the required jar files in the same directory won't do it.
Add a line in your manifest that says:
Class-Path: JAXB2_20081030.jar:JAXB2_20110601.jar:....:netty-3.2.4.Final.jar
or skip the -jar option and launch using
java -cp JAXB2_20081030.jar:....:netty-3.2.4.Final.jar:jRams.jar pkg.JRamsMain
and it should work fine.
(Note that on *nix systems, as opposed to Windows machines, the jar files in the class paths should be separated using : instead of ;.)
Further reading:
The Java Tutorials: Adding Classes to the JAR File's Classpath
You need to add all those JARs to the runtime CLASSPATH by adding the -classpath parameter. AIX requires you to separate the JARs using :
You will have to specify the full path(if libraries not in the same directory as jRams) or just the names of the jar file in a manifest file (If all dependency jars are in the same folder). Alternative specify the path to all the dependent jars using -cp argument.
Example (This assume every dependency is in the same directory you are executing java command from):
java -cp commons-collections-3.2.1.jar; jaxb-impl.jar; etc.. ;jRams.jar package_to_class.MyMainClass.java
Where package_to_class is example: com.myproj.example.
EDITED.
Follow these steps to add "Class-Path" to existing jar file -
Create "newmanifest" file with following entry
Class-Path: additional/jars
Update existing jar file e.g. "classes.jar"
jar --update --manifest=newmanifest --file classes.jar
Inflate jar file
jar -xvf classes.jar
created: META-INF/
inflated: META-INF/MANIFEST.MF
Verify "Class-Path" is added to MANIFEST.MF
cat META-INF/MANIFEST.MF
Manifest-Version: 1.0
main-class: CLASSNAME
Created-By: 15.0.2 (Oracle Corporation)
Class-Path: additional/jars
I have created Excel Sheet using Java program. It works fine.
My problem is, I have copied the .class file into other directory with the necessary jar files need to create this excel sheet, for example
My .class is inside "pack" package.
c:/myprogram/pack/excelprogram.class to d:/myprogram
/pack/excelprogram.class
/jxl.jar
/ojdbc14.jar
If I run the program
javac pack.excelprogram
it display below error
Caused by: java.lang.ClassNotFoundException: jxl.format.CellFormat
I have dependency jar file (jxl.jar) for this Excel sheet creation. Error is displaying from that only.
I have set class path for this jar file like
set classpath="%classpath%";d:/myprogram/jxl.jar;d:/myprogram/ojdbc14.jar;.;
even though I'm getting the same error.
First: You have a class-file (excelprogram.class) if I understand you right. If you want to execute this, you should use java, not javac (that is the compiler to produce the .class-files).
To the question itself: you can specify the classpath on the java-commandline. Try:
java -cp "jxl.jar;ojdbc14.jar;." pack.excelprogram
Clean way of doing this is ,
Make the jar file of your classes , with specified Jar paths in Manifest.mf file which is located in META-INF directory. (Meta-inf) will be created when you create a jar file
Place the dependent libraries( in your case jxl.jar, ojdbc14.jar) in the above mentioned path (the path you mentioned for jar files in manifest.mf)
Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven
Built-By: xxxxx
Build-Jdk: 1.6.0_01
Extension-Name: projectname
Implementation-Title: projectname
Implementation-Version: 1.0
Class-Path: .d:/myprogram/jxl.jar d:/myprogram/ojdbc14.jar
Specify the manifest file with main class manually like below,
Main-Class: MyMainClass
Add the manifest to jar file
jar cvfm myResult.jar myManifest .
Now about Click and Run the jar file :
In Windows file Explorer, choose Tools-> Folder Options..., then select the File Types tab. Check to see if there is a file type of Executable Jar File:
1.Find your javaw.exe file and make a note of its location For example, mine is C:\Program Files\Java\j2re1.5.0\bin\javaw.exe.
2.If there is already a file type of Executable Jar File:
Select Edit for the item.
Select Open as the action.
Select Edit for the action.
Skip to step 4.
3.If there is not already a file type of Executable Jar File:
Select New Type.
For a description, enter Executable Jar File.
For a file extension, enter .jar.
Click advanced button
Under Actions, select New.
In the Action field, enter Open.
4.Change the Application field to:
C:\Program Files\Java\j2re1.4.0\bin\javaw.exe -jar "%1"
where the part before -jar is the path you found in step 1.