I am using a Java external library, a .JAR file that contains a number of classes.
I Have two questions:
I have a problem when using classes in the .JAR file. The problem is when some variables is defined in the class itself, how can I access it? Does the class in the .JAR itself finds it automatically or I should call it?
I would like to know which is best to do: using an external library .JAR file or creating the classes and methods included in the .JAR file and include them in the project I am working on assuming that I have both the source code .JAVA files and the .JAR file of the classes I need to use?
Consider the code below, it is from an external project that I want to use in a current project, I have both .JAR and .JAVA files.
For example the code below has a variable named original_executer that is defined outside this method. If I call this method and give it the required string, will it do its function properly or an error will rise?
public boolean readSet(String setName){
testSet = testSetName;
OriginalLoader myLoader = new OriginalLoader();
original_executer = myLoader.loadTestClass(testSet);
original_obj = original_executer.newInstance();
if(original_obj==null){
System.out.println(" Can't instantiate original object");
return false;
}
return true
}
If you add the .jar to your classpath, you can use everything as if it was defined in your project.
If your .jar file is a external library it is best to keep the library in the .jar and use it from there. Whenever the library gets updated, you can just overwrite the libraries .jar.
given your jar is properly added in the classpath and you have used the necessary imports in your code you can use any class or variables with correct modifier of the jar...
best is to use the external library as jar..and to consume it through package dependency tool like Maven which will automatically download the latest version of jar for you. And then you can compile and run against the latest version
To access the variables defined in the class, you would need to use the getter methods that are supplied. Otherwise, you would need to employ Reflection to grab the values by doing something like
Class.getClass().getField("field_name").set(Class.getClass(), "value");
Although I'm not 100% sure on the validity of that, I'm sure it is something along those lines.
For the second question, I'm not quite sure what you're asking but you should always just add the .jar file to the classpath or if you want to modify the library, download the source code of it and put it into your workspace.
Related
This might be a slightly unusual question but I have a unique situation where something like this is a good solution for me.
I have a jar file (called engine.jar) within my src/main/resources. How can I directly make use of this jar from my main JAVA class? (add this in the build path?) but....
I have another JAR file (of the exact same name i.e. engine.jar) within my Maven Dependencies library which has slightly different code. I want to make use of this jar later on in my main class.
How can I accomplish this in Java?
The name of the jar should not matter by itself. Have you tried renaming 'engine.jar' so there is no conflict and then adding it to the build path?
I would like to know if there is a way to edit the codes in a class file? Because I dont seem to be able to compile a java file into a class file with the use of cmd as it will always detect errors . The Jar file that I am using already have its own existing class files and I would like to update one of the the .class file with a few lines of codes. But when I copied the codes from the class file and put it into a java file together with my added lines of codes, and then try compiling it using cmd, the cmd will generate error whenever I tried to compile it to generate the class file.
Does that mean that I will need to de-compile the whole jar file and make it into a java file and then recompile it into a class file then make it into a new jar file again,? In order to achieve what I am trying to do?
Right now I am using this JCIFS jar file and I would like to add in some lines of codes into one of the class file. I am refering to this "https://code.google.com/p/android-smb-streaming/" as a guide and I discovered that this person is able to customize / add a new class file with the same lines of codes from the existing class in the jar file together with his own added lines of codes.
May I know how do I achieve this? Thank you.
What you have to do here is a common situation: you found a bug/feature request for an open source project.
Instead of hacking it you could try to contact the author of the library, submit your contribution and wait until it gets a new release so it has your new cool feature.
On the other hand, you can use the exposed API to customize it for your needs, e.g., create a specific subclass that overrides the required method containing that few lines of code and use the API in a way that it uses your implementation.
If you want to go the hacky way, there's no need to manipulate the classfiles directly (it is far more complicated): the easy way is to download the project, read the docs how to set it up (so you can compile it with cmd w/o errors), add your patch, compile it, then you can update the used JAR file with the new class file (or files: note that there might be multiple files generated if you're using inner classes or lambdas). Just don't forget to mention that somewhere in the docs, because otherwise no one will ever know that the library is slightly modified...
In my project we are referencing lot of dependency .jar files.
/lib/xxx.jar
/lib/abc.jar
The xxx.jar file having some (com.search.hit) packages. The same packages are available in abc.jar file.But the problem comes into picture now, where accessing xxx.jar file it doesn't referencing their package(com.search.hit) instead it is referencing abc.jar package.
Could anyone tell how to redirect the flow?
The class from whichever jar comes first on the classpath is the one that is used. The other one might not even exist as far as the classloader is concerned. It's a good idea to avoid conflicts like this.
In Eclipse go to your project build path configuration and click on the Libraries tab.
Then remove the package that you don't want to be accessed from your list and add it again.
This will cause the package to be lower in the priority list and it'll check the other package before the one you just re-added.
This will create problems for you. My suggestion would be to create 2 extra classes for writing getter and setter wrappers for these jar files. Write 2 seperate classes, each one of them will reference just one of them and your project file will use these wrapper classes to invoke functions from these jars. It would be a lot easier that way.
You have to change the package name it can not be same file name with same package in single JVM. JVM will take load randomly one jar class using class loader and ignore the rest.
I have a runnable java jar file that I need somehow to run (pass params, fetch output) from another java class I'm working on. How do I do that? Do I import it as a package somehow, call it on runtime? Can I invoke "main" method from it or do I just run it with "exec"? Thanks for your answers.
Simply add it to your CLASSPATH and call either the main() method or any other public method which it provides (and which is documented). There is no difference between "normal" and "runnable" jar files, besides an entry in the manifest.
One subtle detail you might need to consider is that runnable jar files are usually self-contained - that is, they contain all required classes, including classes from third party libraries. If you are using the same third party libraries in your project, make sure that there are no conflicts, e.g. by removing the separate third party jar files from your project.
See Lesson: Packaging Programs in JAR Files for more information.
Import it in your code if you need something, that you can not achieve from command line exec solution. Note, you can get problems, if you trying to use some methods from jar file itself, and author changes it later.
Consider Bridge pattern, to put some abstraction layer between jar file and your code, If you not sure, that you use public API.
If jar file is library with stable API, you can be more confident, while using it in your project.
Besides, importing jar file and use methods from it is faster, than parsing process output.
u can import it to your project and then call the publicly exposed methods in that jar from within your application.
I am programming in java using Eclipse, I am a novice and I'm stuck with the following problem: I have an Eclipse project that uses an external jar library. In this library there is a specific class that needs to be temporarily modified. I have the source code for this class. Is it possible to somehow include the source file for the code into the project, so that it will "override" the corresponding class in the jar file?
Thank you.
Basically, it's not possible to have two classes with the same signature (package + name) in the classpath but it's possible to import this class in your project in different package and use it instead of the original one.
Another way to solve this problem is to edit the .jar file by removing or changing the class that you need to be different.
However, note that changing an API is almost never a good idea.