Nicky
Nicky

Reputation: 63

How to add your own .class into a compiled .jar file?

I have a .jar and I would like to add my own .class to it and recompile. Is there anywhere I can do it?

Upvotes: 3

Views: 13150

Answers (1)

Borut Hadžialić
Borut Hadžialić

Reputation: 371

Yes, and it's simple (if you already have the .class file - then there is no recompilation).

(Assuming you have the .class file already and just want to add it to a .jar. If you don't have the .class file, you need to write a .java source file and compile it to .class first using javac)

Jar files are actually zip files - you can use zip/unzip to create and unpack them.

  1. unzip the jar file using a unzip program
  2. add your class to the unzipped directory (*)
  3. zip again (possibly to another name.jar)

(*) In step 2, you must put your .class file in the correct directory - correct meaning that the package name of your class must match the directory path where you .class file resides, relative to the .jar archive root.

For example if your My.class defines that it is in package com.nicky, then it must be found at com/nicky/My.class (where com is a directory in the archive root directory). If My.class has no package, they it must be in archive root directory.

No recompilation is needed - Java does linking dynamically at runtime - if the rest of the program needs to use class com.nicky.My, it will do so successfully if the class file is in the correct place in the .jar file.

You use the classpath parameter of the virtual machine (java -cp a.jar:b.jar:... ) to tell the java process where to look for classes - in case it would make more sense to package your class(es) in a separate .jar file..

-- Edit 1 / responding to your comment: In case you a writing a new .java file, you need to first compile it into a .class using the javac command line compiler that comes with the jdk.

Every Java class belongs to a package, which is usually declared in the first line of the .java source file. Package names must match directory path location of the class (like in the example above), for both .java source files (for compilation to succeed), and for .class files (for dynamic loading / running to succeed).

You could also download and use a Java IDE (eg. http://www.eclipse.org/downloads/packages/eclipse-ide-java-developers/mars2 )to create your .class file. Create a new Java Project in Eclipse, write your class source code, click build (if it already doesn't build automatically) and then use a file explorer to take your .class file from the 'target' directory of the project on the disk.

If your new .java class depends on classes from the .jar you are trying to add it to, use

javac -cp your.jar com/nicky/My.java

to tell the compiler where the additional compile-time dependencies are.

If you are compiling from Eclipse, you need to configure that your project depends on your.jar: Right Click on your Project -> Properties -> Java Build Path -> Libraries -> Add Jars..

Upvotes: 9

Related Questions