I need to add a class to a .jar library but I can't figure out how to do it.
I have a library named netty-3.1.5.GA.jar but for some reason its missing a class I need
(HttpTunnelClientChannelFactory.java).
I have found that class on a repository but not as part of the library. So how can I 'inject' it? The class I need to add is using other classes that exist in the library.
You don't need to add it to the jar, you need to add it to the classpath of whatever it is you're running.
You can use the jar tool to update the jar file: you will need to manually create the appropriate package directory though. Try this:
jar uf netty-3.1.5.GA.jar HttpTunnelClientChannelFactory.class
will add it to the root package. If you need it to be set up in a package directory you should create the directory structure and then add the file with a path.
EDIT: that should be .class, not .java.
Related
I have two classes:
MyApplication Library
The Library has already been compiled into Library.class and the source code is no longer available. I am now trying to compile MyApplication from source. MyApplication depends on the Library. The Library has a package name of org.myCompany. I tried setting my classpath to the following:
set CLASSPATH=C:\java\project\org\myCompany\Library.class;.
which produced the following javac compiler error message:
MyApplication.java:33: cannot find symbol
symbol: class Library
location: class MyApplication
Library theLibrary = new Library();
So I changed my classpath to be:
set CLASSPATH=C:\java\project\;.
which produced the exact same error message.
How do I set my Windows classpath to include the Library.class file? Should it point at the folder contains the org\myCompany subfolders? Or point directly to the class file? Or to the folder containing the class file (even though the class is in a package and belongs in a subfolder)?
I do an echo %CLASSPATH% after my set command and the classpath is being set correctly. I also made an ant build.xml file and encountered the same problem. In fact, ant -verbose confirmed that my classpath is being set correctly.
First of all: the use of the CLASSPATH environment variable is very strongly discouraged. The best thing is for you to forget that it exists. Use the -cp command line switch or similar methods to set the classpath.
Second, the classpath entries each represent a place where the classloader will start looking for .class according to the package hierarchy, i.e. it will look for the class org.myCompany.Library in a subfolder org/myCompany in any of the classpath entries.
Therefore, if
you add a classpath entry C:\java\project\
and there is a class file C:\java\project\org\myCompany\Library.class
which is actually part of a package org.myCompany (capitalization matters here!)
and your MyApplication class has an import org.myCompany.Library;
Then it really should work.
You cannot add a single class in your classpath like this. You have 3 solutions:
add this class in the path of your other compiled classes (respecting the package naming of your directories)
add the root directory of this class in your classpath (in your case "C:\java\project\")
add this single class into a jar and add this jar to the classpath
For your problem, the thrird choice is cleaner: external dependencies normally are packaged into jar files.
If your .class file isn't in jar file, point your classpath to the parent dir where package of class resides, e.g., for class org.myCompany.Library, point your CP to directory containing org/myCompany.
If your .class file included into some jar file, add full path to that jar to your classpath.
If you compiled the class files to a different directory, the classpath needs to point to where the .class file is.
set CLASSPATH=C:\java\project\;
is correct assuming that that the class file is in the same directory as the .java source file.
Is there a problem locating the Library to the same root project where is your MyApplication class
Example, if:
c:/project/org/company/MyApplication.class
Can you locate the Library class into:
C:/project/org/myCompany/Library.class
please notice, that the folders org/myCompany and org/company are located under the same folder c:/project/.
Please notices that this solution works for you if the Library Class is only used by your application.
Edited
Windows command prompt is tedious, after setting the classpath please close and re-open the Command Prompt, so it can see the new classpath's value.
For the classpath to work, you need to have a folder structure which matches the package hierarchy. So if your class is org.myCompany.Library, you must create a nested folder structure of C:\java\project\org\myCompany and place your Library class file in the myCompany folder. Then set the class path to C:\java\project\
I use java instrument(premain) to change bytecode,i used losts of jars,like asm,spring,guava.When i package this agent to a jar and run this jar on a springboot's,it always tell me java.lang.NoClassDefFoundError.I know i can add Boot-Class-Path to the manifest file or use instrument's api to add jar,but is there an quick way to add jars to javaagent's classpath?
I'm working on creating my own implementation of one of the system Java packages but am having some problems with the wrong class getting picked up when trying to use the package.
For example, lets say my package is: a.b.c.DoStuff and there is an existing Java package with the exact same name, a.b.c.DoStuff.
Using the following code in a test application, I can tell that the system class is still getting used (located in /usr/lib/jvm/java-6-openjdk/jre/lib/rt.jar) instead of my own:
ClassLoader loader = test.class.getClassLoader();
System.out.println(loader.getResource("a.b.c.DoStuff.class");
My package has been compiled into a jar file (package.jar), and I have:
Imported the class in my test file (import a.b.c.DoStuff;)
Added package.jar to my classpath (with both "export CLASSPATH..." and using "java -classpath...")
There must be something I'm overlooking? Any thoughts on how to get my package picked up instead of the system package?
Thanks,
Chris
You can't replace classes from the standard packages unless you put your jar in the special 'endorsed' directory. And some you can't replace at all that way.
Thanks, bmargulies for the tip on the bootclasspath! Prepending my .jar file to the bootclasspath solved my problem. To summarize, I was able to use my own Java package implementation by prepending my package's .jar to the bootclasspath:
java -Xbootclasspath/p:<path-to-jar>
I need to patch a class that is in a JA file, in a package following the form pack1.pack2.pack3.com. So the full name of class is:
pack1.pack2.pack3.com.classtopatch
I have a folder in my application:
c:/app/patches
Should I simply copy the class to c:/app/patches/classtopath.class file or do I
need to create the entire folder tree like:
c:/app/patches/pack1/pack2/pack3/com
and in pack1/pack2/pack3/com put classtopath.clas ?
Thanks.
Yes, you need to create the directory structure to match your package name.
Create c:/app/patches/pack1/pack2/pack3/com and put classtopath.class in it.
Your question isn't really clear, but if you've got class files on the file system, then they should be in the appropriate package structure.
If you're packing it into a jar file then the jar file itself can be anywhere, but the package structure should be present in the jar file.
i want to access a class from another project in the workspace using ClassLoader. How can i specify the path to that class and get that class file ?I am using Eclipse . Help
You have to just add that project in to build path configuration of the project where you want to access that. And the class loader will find that class depending on the import statement you have given in the class where you are using it.
To add in the build path : you have to right click on the project > select Build Path > Configure Build Path > then select a project tab and add the project in which the class is present which you want load.
You could try URLClassLoader an example is here
I never used GNU Classpath which could be heplful.
While I'm sure there are devious ways of doing exactly that, I think having one project reference another this way is not the best of ideas.
What you can do is create a jar of the project you're getting the class from via Export->Java->JAR file and put that file into your project. This will let you access the class you need while still keeping your projects self-contained.