Manu
Manu

Reputation: 3247

How to run the dependecy class file in 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.

Upvotes: 1

Views: 1206

Answers (3)

srinannapa
srinannapa

Reputation: 3207

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.

Upvotes: 0

srinannapa
srinannapa

Reputation: 3207

Clean way of doing this is ,

  1. 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

  2. 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

Upvotes: 0

Mnementh
Mnementh

Reputation: 51311

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

Upvotes: 3

Related Questions