How to create globally available libraries in Java? - java

What I mean is, in Java there is a standard set of packages that can be referenced from any program in any context on the computer. So when I want to process raster images I just add import java.awt.image.BufferedImage to the top of my file and I able to use that pre-built class without referencing the actual java.awt.image package files at all during compilation and run time. Where are these globally importable packages kept and/or how can I make my package files globally available in the same way?
I have been writing a lot of small helper programs lately, most of which do very similar things, and it would be convenient for me to be able to reuse code without explicitly referencing my package files.
Thanks for any help.

you CAN place any jar library you want into your JRE to achieve same result - classes from this jar will be available in classpath at runtime though it is not recommended to do so without a reason :) But if you are just learning and do not want bother with build tools it's OK
[java-home]/lib/ext - standart place for your libraries
[java-home]/lib/endorsed - place for the libraries which API's overwrite default JRE's ones

Related

Are there tools for culling unused classes from a jar file or set of jar files for a java applicaton

So we have an extensive library of java code in a number projects that make jar files and external libraries. I am working on an app that uses a sparse set of classes from the large set of jar files in the class path.
Because we are loading the app on a device with limited storage I would like to cull all the unused classes from the jar file set and then create a jar of only what is needed for the application.
That is run a process very similar to a traditional "link" that only includes code that is actually referenced.
Ideally I could specify a list of "Root" classes like the one including the application's "main" and anything I know is dynamically loaded and items such as resources, and run some tool on a specified class path which will build a folder of just what is needed to run the application.
Then I can jar up that result for the as-small-as-possible jar needed to run the app/applet/servlet.
I could conceive of a custom "class loader" that would simply generate a listing of everything that is loaded when an app runs. (ie class + source in classpath) that could be used for this purpose if one was able to run all code paths in the application. Of course if there is dynamic loading of platform or other runtime dependencies this would not cover for that but it would do a large part of the job.
Thanks.
One such tool is ProGuard.
ProGuard is a free Java class file shrinker, optimizer, obfuscator, and preverifier. It detects and removes unused classes, fields, methods, and attributes. It optimizes bytecode and removes unused instructions. It renames the remaining classes, fields, and methods using short meaningless names. Finally, it preverifies the processed code for Java 6 or higher, or for Java Micro Edition.
Tutorial for using it is here.

How to monkeypatch a .class file in a jar

I have a jar file that cannot be modified, but I want to use a different .class file in place of one of the members of the jar. How can I tell Java to use the external .class file when the code within the jar attempts to load it?
You could compile another jar file with replacement classes with exactly the same name and put it ahead of the jar file in the class path. For example, this is what the various slf4j bridge jars do to replace calls to log4j or Jakarta Commons Logging in library code with cognate slf4j code; one need not maintain two sets of logging systems and configurations that way.
If you want to override a java... class, you can use some of the command line options to change the boot class path. Look at the -Xbootclasspath options in http://docs.oracle.com/javase/7/docs/technotes/tools/windows/java.html. Heed the warnings.
There is also the lib/endorsed directory if you need to upgrade a third-party jar that Sun uses. Oracle uses other organizations' XML and CORBA libraries; if they release a new version and you need to adopt it, you can.
You can use AspectJ to instrument the code and possibly replace it. An around advice can call the original code if it wants.
You could see if you really need to replace the original code. Some systems provide customization hooks.
You need to make sure the external .class file is loaded first. If a class is already loaded by the class loader then it will not be reloaded. If you are using an application server, then there are ways to configure the preferences for loading classes for class loader. But if you are using a standalone application then you may need to extend the class loader to load the files in the order you want to.

How to use runnable jar in another class?

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.

jar libraries and separate .java files in Eclipse

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.

Import classes from folder

I am new to android and i want to import some classes to my application wich are placed in MyClasses folder in same package.
After googling for a time I found a way to do this by usig Java build path and in this add class folder. I think this may solve my problem but no luck.
I don't know how to import classes from folders. Is it possible to import classes from folder? If it is possible then how to do this?
Any help will be appreciated.
I assume this is for Eclipse. You have the options for importing a Class Folder, which is not very useful in my opinion since it only loads folders in projects you have defined in your workspace. In this scenario I would just link to the project itself. But you can also import External Class Folders. If this is what you are trying to accomplish then make sure you are using the correct item in the Java Build Path dialog.
The problem that Dalvik does not understand java files. They should be compiled into the internal Dalvik format dex. And even after that I'm not sure that you can dynamically load files into your project.
Have the folder in your build path, then use:
Class.forName("package.className")
It will also call the static constructor for the classes loaded this way (this pattern is used with JDBC drivers).
If you want to change the folder at runtime, you might have to write your own class loader.
This might help more: http://www.techrepublic.com/article/get-the-most-out-of-javas-class-loaders/6080883
Not sure how this works with Android, though. I don't think you can implement your own class loader for Android due to security issues.

Categories