How to run the dependecy class file in Java - java

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.

Related

Unable to execute jar file due to main manifest attribute missing

I have below project structure:
->bin
->lib
->resources
->src
->DemoFramework
->FirstDemo.java
In lib folder, I have an external jar that I need in my application. Its name is ext.jar. In resources, I have Manifest.txt file, whose content is given below.
Manifest-Version: 1.0
Class-Path: . lib/ext.jar
Main-Class: DemoFramework.FirstDemo
I am using below command to generate jar:
javac -cp ".;./lib/ext.jar" src/DemoFramework/*.java -d bin
Basically I am putting all class files into bin folder so that in final jar file, source code is not visible.
Then I am issuing below command:
jar cmf resources/Manifest.txt project.jar bin lib
The jar file is successfully created but when I run it, it says:
no main manifest attribute, in project.jar
I am confused about this error, no idea why it is happening.
Can you guys help me to sort it out?
Thanks.

jar file doesn't execute

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.

Jar executable with sqlite driver

I made a code that connects to my sqlite driver which is in the CLASSPATH and reads some database file. I want to create an executable which can be used on computers that don't have the sqlite driver.
If I do:
jar cvfe exec.jar main_class
I will get "class not found: org.sqlite.JDBC" when running with
java -jar exec.jar
What should I do to make the executable work?
Edit:
I don't know if it makes any difference, but this is the JDBC driver I use:
https://bitbucket.org/xerial/sqlite-jdbc
You need to include the library inside the JAR. Maybe you don't know this, but JAR files are just ZIP files, so you can change their contents easily. Here are some quick instructions on how to do it. Assuming your JAR file is named exec.jar, and the JAR of the library you want to include (the JAR you downloaded) is driver.jar
Change your file name from exec.jar to exec.zip.
Extract all the contents of exec.zip into folder exec/
Change your library file name from driver.jar to driver.zip
Extract all the contents of driver.zip into folder driver/
Copy the contents of driver/ into exec/, but do not copy the META-INF folder. If a pop-up asks if it's ok to merge the folders, click yes.
Compress all files in exec/ into exec.zip
Rename exec.zip to exec.jar (replace the original).
You can include any java library inside a JAR using this method.
Here is the doc:
C:\Windows\System32>jar /?
Illegal option: /
Usage: jar {ctxui}[vfmn0Me] [jar-file] [manifest-file] [entry-point] [-C dir] files ...
And so I think the command you need is:
jar cvfe exec.jar main_class main_class

Reference jars inside a jar

I have a jar whose content looks as shown below,
Below is my manifest file
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.8.3
Created-By: 1.7.0_06-b24 (Oracle Corporation)
Main-Class: org.sai.com.DerbyDemo
Class-Path: derby.jar derbyclient.jar derbynet.jar derbytools.jar
When i try to run the jar, it has thrown a ClassNotFoundExcception meaning it isn't referencing the jars inside the outer jar.
In the Class-Path attribute, how can I reference jars (derby.jar, etc) inside the actual jar?
You will need a custom class loader for this, have a look at One Jar.
One-JAR lets you package a Java application together with its dependency Jars into a single executable Jar file.
It has an ant task which can simplify the building of it as well.
REFERENCE (from background)
Most developers reasonably assume that putting a dependency Jar file into their own Jar file, and adding a Class-Path attribute to the META-INF/MANIFEST will do the trick:
jarname.jar
| /META-INF
| | MANIFEST.MF
| | Main-Class: com.mydomain.mypackage.Main
| | Class-Path: commons-logging.jar
| /com/mydomain/mypackage
| | Main.class
| commons-logging.jar
Unfortunately this is does not work. The Java Launcher$AppClassLoader does not know how to load classes from a Jar inside a Jar with this kind of Class-Path. Trying to use jar:file:jarname.jar!/commons-logging.jar also leads down a dead-end. This approach will only work if you install (i.e. scatter) the supporting Jar files into the directory where the jarname.jar file is installed.
You can't. From the official tutorial:
By using the Class-Path header in the manifest, you can avoid having
to specify a long -classpath flag when invoking Java to run the your
application.
Note: 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. To load classes in JAR files within a JAR
file into the class path, you must write custom code to load those
classes. For example, if MyJar.jar contains another JAR file called
MyUtils.jar, you cannot use the Class-Path header in MyJar.jar's
manifest to load classes in MyUtils.jar into the class path.
In Eclipse you have option to export executable jar.
You have an option to package all project related jars into generated jar and in this way eclipse add custom class loader which will refer to you integrated jars within new jar.
Default implementations of the classloader cannot load from a jar-within-a-jar: in order to do so, the entire 'sub-jar' would have to be loaded into memory, which defeats the random-access benefits of the jar format (reference pending - I'll make an edit once I find the documentation supporting this).
I recommend using a program such as JarSplice to bundle everything for you into one clean executable jar.
Edit: Couldn't find the source reference, but here's an un-resolved RFE off the Sun website describing this exact 'problem': http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4648386
Also, you could 'test' that your program works by placing the library jar files in a \lib sub-directory of your classes directory, then running from the command line. In other words, with the following directory structure:
classes/org/sai/com/DerbyDemo.class
classes/org/sai/com/OtherClassFiles.class
classes/lib/derby.jar
classes/lib/derbyclient.jar
From the command line, navigate to the above-mentioned 'classes' directory, and type:
java -cp .:lib/* org.sai.com.DerbyDemo
if you do not want to create a custom class loader. You can read the jar file stream. And transfer it to a File object. Then you can get the url of the File. Send it to the URLClassLoader, you can load the jar file as you want.
sample:
InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream("example"+ ".jar");
final File tempFile = File.createTempFile("temp", ".jar");
tempFile.deleteOnExit(); // you can delete the temp file or not
try (FileOutputStream out = new FileOutputStream(tempFile)) {
IOUtils.copy(resourceAsStream, out);
}
IOUtils.closeQuietly(resourceAsStream);
URL url = tempFile.toURI().toURL();
URLClassLoader urlClassLoader = new URLClassLoader(new URL[]{url});
urlClassLoader.loadClass()
...
Add the jar files to your library(if using netbeans) and modify your manifest's file classpath as follows:
Class-Path: lib/derby.jar lib/derbyclient.jar lib/derbynet.jar lib/derbytools.jar
a similar answer exists here
in eclipse, right click project, select RunAs -> Run Configuration and save your run configuration, this will be used when you next export as Runnable JARs

Cannot find class even when jar file is in working directory

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

Categories

Resources