I've written a java program that reads an excel file with jxl.jar. It's currently working, but I have to use cmd to run the program. Double clicking the jar file does not appear to be working. These are the commands I use to compile and run the code:
javac -classpath C:/workspace/jxl.jar:. main.java GUi.java
jar cvfm run.jar manifest.txt Main.class GUI.class GUI$1.class GUI$2.class GUI$3.class Main$1MyCustomTableCellRenderer.class Main$1YourTableCellRenderer.class Main$MyCustomTableCellRenderer.class
java -cp run.jar Main
I'm not really sure why it's any different from double clicking it. I've compiled the jxl file into the run.jar file so I don't understand why it doesn't work?
An Example directly from Java Tutorial on Adding Classes to the JAR File's Classpath as suggested by #MadProgrammer in his comment.
We want to load classes in MyUtils.jar into the class path for use in MyJar.jar. These two JAR files are in the same directory.
We first create a text file named Manifest.txt with the following contents:
Class-Path: MyUtils.jar
Warning: The text file must end with a new line or carriage return. The last line will not be parsed properly if it does not end with a new line or carriage return.
We then create a JAR file named MyJar.jar by entering the following command:
jar cfm MyJar.jar Manifest.txt MyPackage/*.class
This creates the JAR file with a manifest with the following contents:
Manifest-Version: 1.0
Class-Path: MyUtils.jar
Created-By: 1.7.0_06 (Oracle Corporation)
The classes in MyUtils.jar are now loaded into the class path when you run MyJar.jar.
Read more...
Try this in command prompt:
Run the below command if you bundled the jar with all libraries
java -jar MyJar.jar
If you need to add jars at the time of execution please add all the jars at command line, as follows;
java -cp /path/jxl.jar;myJar.jar Class-Name-Main
Related
i am want create jar of my project using cmd line.
I am using ini4j libs.
Everything compiles fine, but I do not know how to set the -cp to the library.
Compile:
javac -cp ".;lib/ini4j-0.5.2.jar;ini4j-0.5.2-jdk14.jar;lib/ini4j-0.5.2-jdk14.jar" gui_Frame/*.java
Create jar:
echo Main-class: gui_Frame/MainApp > manifest.txt
jar cvfm GVE.jar manifest.txt gui_Frame/*
But, if i want start java -jar GVE.jar i get following error:
Java.lang.NoClassDefFoundError: org/ini4j/wini
What am I doing wrong ?
You must specify the same classpath when running java as you did when compiling.
Or bundle all the necessary class files in one JAR.
==EDIT==
Try this:
java -cp "GVE.jar;.;lib/ini4j-0.5.2.jar" gui_Frame.MainApp
I have a java project that has 10 .java files. A1.java, A2.java......A9.java and Main.java.
The project contains a package grand.big.medium.small and all the .java files are inside this package.
I have compiled and created an executable jar file for the project using:
javac grand/big/medium/small/*.java
to run the project I used the command
java -cp . grand/big/medium/small/Main
to create the jar I used the command
jar -cvmf manifest.txt MyJAR.jar *.class
I want to run the jar file from command prompt and I am using the commad
java -cp . -jar grand/big/medium/small/MyJAR.jar
and I getting an error:
Error: Could not find or load main class Main
please can anyone tell me why I am getting this error?
Run as:
java -cp . -jar grand/big/medium/small/MyJAR.jar grand.big.medium.small.Main
Assuming you have grand as a directory within current directory and directory hierarchy follows.
I am working with pre-commit hook developed in java. Now i need to run it inside pre-commit.
List item
How can i run jar file with dependency?
Do I have to make executable jar file to run from pre-commit?
How to give classpath and parameter to jar file?
Currntly i am ruuning in this way inside pre-commit which is working fine. Now i need to move it to production
/System/Library/Frameworks/JavaVM.framework/Versions/1.6/Home/bin/java -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=11111
$JAVA_OPTS -Dfile.encoding=MacRoman
-classpath
/System/Library/Frameworks/JavaVM.framework/Versions/1.6/Home/lib/deploy.jar:
/System/Library/Frameworks/JavaVM.framework/Versions/1.6/Home/lib/dt.jar:
/System/Library/Frameworks/JavaVM.framework/Versions/1.6/Home/lib/javaws.jar:
/System/Library/Frameworks/JavaVM.framework/Versions/1.6/Home/lib/jce.jar:
/System/Library/Frameworks/JavaVM.framework/Versions/1.6/Home/lib/jconsole.jar:
/System/Library/Frameworks/JavaVM.framework/Versions/1.6/Home/lib/management-agent.jar:
/System/Library/Frameworks/JavaVM.framework/Versions/1.6/Home/lib/plugin.jar:
/System/Library/Frameworks/JavaVM.framework/Versions/1.6/Home/lib/ext/sunjce_provider.jar:
/System/Library/Frameworks/JavaVM.framework/Versions/1.6/Home/lib/ext/sunpkcs11.jar:
/Users/testuser/Documents/Dev/projects/SCS/sc/SCS/SCS/SCSAPI/classes:
/Users/testuser/Documents/Dev/projects/SCS/sc/g3/_lib/hibernate-tools.jar:
/Users/testuser/Documents/Dev/projects/SCS/sc/g3/_lib/dom4j-1.4.jar:
/Users/testuser/Documents/Dev/projects/SCS/sc/g3/_lib/commons-logging.jar:
/Users/testuser/Documents/Dev/projects/SCS/sc/g3/_lib/commons-collections-2.1.1.jar:
/Users/testuser/Documents/Dev/projects/SCS/sc/g3/_lib/mysql-connector-java-5.1.22-bin.jar:
/Users/testuser/Documents/Dev/projects/SCS/sc/g3/_lib/hibernate2.jar:
/Users/testuser/Documents/Dev/projects/SCS/sc/g3/_lib/ehcache-1.1.jar
com.ticoon.scs.SCSMain
pre-commit file -c /Users/testuser/Documents/Dev/projects/SCS/sc/SCS/SCS/config.xml -v -repos $REPOS -trxn $TXN
Do I have to make an executable Jar file?
You don't need to create a runnable Jar, but that is advised because it make things easier.
Basically if you run a Jar file, you have to specify the class containing the main() method:
java -jar myjar.jar x.y.z.Main
If you have a Main-Class: x.y.z.Main attribute defined in your MANIFEST.MF file, you can run the Jar file by simply:
java -jar myjar.jar
How to give classpath to a Jar file?
When you run something with the java -jar ... command, the -classpath ... property is dropped. The reason is security (Jar files can be digitally signed to ensure they weren't modified and it would be so easy to make java load a different dependency Jar file with the same content but with hijacked functionality).
The solution is simple: include the Class-Path: ... attribute too in your MANIFEST.MF file as you do with Main-Class: ....
How to give parameters to a Jar file?
As you would give parameters to a simple Java program (and the manual specifies it):
java -jar myjar.jar arg1 arg2 ... argN
Yehh its working now with single file. thanks #rlegendi
Working copy
REPOS="$1"
TXN="$2"
java -jar /Users/testuser/Documents/SCS/SCS.jar pre-commit file -c /Users/testuser/Documents/SCSAPI/config.xml -repos $REPOS -trxn $TXN
I have created a runnable jar file that contains the following classes:
Main.class (This contains the main method)
Form.class
WrapLayout.class
The manifest file (Manifest.txt) contains the following:
Main-Class: Main
(It has a new line at the end)
I have created the jar file using this command:
jar cfm Organizer.jar Manifest.txt Main.class Form.class WrapLayout.class
The jar file is created, but when I run it, the command window opens up for a split second, closes, and nothing else happens. The file is a swing application, and I am running Windows XP on Parallels Desktop for Mac, but I don't think that matters. Does anyone know what's wrong?
EDIT: I tried running the jar file in mac, and I got this error message in the console:
Exception in thread "main"
java.lang.UnsupportedClassVerionError: Main : Unsupported major.minor version51.0
follow these steps an test:
Start Command Prompt. Navigate to the folder that holds your class
files: C:>cd \mywork
Set path to include JDK’s bin. For example: C:\mywork> path
c:\Program Files\Java\jdk1.5.0_09\bin;%path%
Compile your class(es): C:\mywork> javac *.java
Create a manifest file: C:\mywork> echo Main-Class: YourMainClass >manifest.txt
Create a jar file: C:\mywork> jar cvfm YourMainClass.jar manifest.txt
*.class
Test your jar: C:\mywork> YourMainClass.jar
I'm trying to run a jar ec/mobat/MOBAT.jar which depends on some jars located in ec/mobat/lib/. It works if I do:
ec/mobat/$ java -jar MOBAT.jar
However I want to be able to run the jar from another directory
ec/$ java -jar mobat/MOBAT.jar
But I get an exception
java.lang.NoClassDefFoundError: ibis/io/Serializable
...
I tried to pass the required jars in the classpath
ec/$ CLASSPATH=... java -jar mobat/MOBAT.jar
ec/$ java -jar -cp ... mobat/MOBAT.jar
but I get exactly the same exception. Any fix?
Update: MANIFEST.INF contains the following:
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.7.0
Created-By: Selmar Kagiso Smit
Main-Class: mobat.Launcher
Implementation-Version: 1.3.4
The classpath has to contain every jar you're depending on.
java -classpath b.jar;c.jar -jar a.jar //does not work see below
The ";" is system dependent for windows ":" for unix.
The jar switch is used to select the jar file whose main class is executed (Main-Class: mobat.Launcher in the manifest file). The command line:
java -classpath b.jar;c.jar;a.jar mobat.Launcher
Would produce the same result.
Alternatively classpath definitions can be added to the Manifest file. Your manifest file could contain the attribute.
Class-Path: lib/b.jar lib/c.jar
Then
java -jar a.jar
would work.
Edit:
I thought that -jar and -cp could be used together. But the java tools documentation is clear:
-jar
When you use this option, the JAR file is the source of all user
classes, and other user class path
settings are ignored.
Only the manifest and everything explict (classpath and main class) versions work.
You can not use -cp and -jar together
java -cp myjar.jar;lib/*;. mypackage.MyClass
shall work on windows and
java -cp myjar.jar:lib/*:. mypackage.MyClass
shall work on Unix