creating META-INF/services folder in Eclipse - java

I am trying to create an extensible app in Java and chose to use SPI. According to this tutorial I am following, I am supposed to create a specific file in META-INF/services in my jar, but don't know how.
I know how to create jar with META-INF, but can't find a way how to create the services folder and particular file in this folder. (I am using Eclipse)
Thanks for your help.

Since the tutorial is instructing to create it manually rather than using ant or maven or other build tools then just create a folder named META-INF at the root level with a sub-folder named services and a file called MANIFEST.MF.
I am not sure how you plan to execute your jar file, for now you can just put this one line in your MANIFEST.MF:
Manifest-Version: 1.0
For your services folder, you need to create a file named com.example.dictionary.spi.Dictionary with the following one line content as mentioned in the tutorial:-
com.example.dictionary.GeneralDictionary
FYI - META-INF is an internal Java meta directory. Generally, you want to rely on your packaging tool such as ant or maven to build the content of META-INF folder rather than doing it by yourself.
You can see the details on the content of META-INF folder here:-
The following files/directories in the META-INF directory are recognized and interpreted by the Java 2 Platform to configure applications, extensions, class loaders and services:
MANIFEST.MF
The manifest file that is used to define extension and package related data.
INDEX.LIST
This file is generated by the new "-i" option of the jar tool, which contains location information for packages defined in an application or extension. It is part of the JarIndex implementation and used by class loaders to speed up their class loading process.
x.SF
The signature file for the JAR file. 'x' stands for the base file name.
x.DSA
The signature block file associated with the signature file with the same base file name. This file stores the digital signature of the corresponding signature file.
services/

Assuming you are building your jar file with Apache's Ant, you need to add a metainf fileset, or better yet, use a services directive like so
<jar jarfile="pinky.jar">
<fileset dir="build/classes"/>
<service type="javax.script.ScriptEngineFactory"
provider="org.acme.PinkyLanguage"/>
</jar>
If you don't use apache's Ant, then you need to add a file to your jar
jar -uf jarfile.jar META-INF/services/name.of.general.service
which contains the name of the service's implementation
com.mycorp.services.Fooservice
If you are generating the JAR file using Eclipse's "Runnable Jar file export" under the project, select "save as ANT script" and then modify the ant script to pack in the services as described above.

Related

How to Import an External .class File in an Eclipse Java Project

I am trying to include a .class file through an import in a .java class I am creating. I need the .java class to look the same as what I need to deploy on the application server. I have created a directory structure to put the Context.class file in.
I updated the build path by using "Add External Class Folder..." and selected the "oracle" directory containing apps>fnd>common.
However, Eclipse still can't find the Context.class file referenced in the import.
What am I doing wrong?
You need to select the classes (C:\Users\badams\java\classes) directory instead of the oracle directory when using Add External Class Folder....
You should provide the root of the directory that represent your java package structure.
As an alternative, you can create a jar archive with the contents of the C:\Users\badams\java\classes directory and add that archive as a jar dependency as well.
You can use for this purpose the builtin SDK jar utility. Please, change you current directory to C:\Users\badams\java\classes and run from a command prompt or PowerShell terminal something like:
jar cvf oracle-fnd.jar *
Then add the generated archive to your project as usual.

Javadoc out of the jars that have a java file inside

I have many jar files and each of them have a .java file inside the folder tree that contains the description of method used in a class. As I export each jar to the build path in Eclipse, the help is also available. Is there a way to automatically generate one big javadoc out of all those jars?
Any ideas?

JAR doesn't include root files

I have simple Java Application and trying to create JAR to distribute using eclipse.
But when I look inside JAR it doesn't contain the test.txt file. I created JAR as Export>Runnable JAR File
You need to put that file into one of your source folders (such as src). Only those get copied into the jar file (by default).
See the following image:
Choose the file which you want to add in jar file.

How to convert AAR to JAR

Situation:
I intend to use a Java library and I only have an AAR file from a Maven repository but I need the JAR file.
Background story:
I tried to compile a library, but its Gradle structure was unstable. So I asked for a compiled jar file, and its developer handed me an aar file from some Maven repository (the developer couldn't compile his own project).
The Gradle configuration was a mess, it depended on multiple libraries and Gradle has thrown some exceptions.
I tried to use it in my IDEA IDE, but it couldn't see it. A library project should be able to be compiled into a jar file, right?
Question:
What should I do to convert that AAR file to a JAR file?
The AAR file consists of a JAR file and some resource files (it is basically a standard zip file with a custom file extension). Here are the steps to convert:
Extract the AAR file using standard zip extract (rename it to *.zip to make it easier)
Find the classes.jar file in the extracted files
Rename it as you like and use that jar file in your project
.aar is a standard zip archive, the same one used in .jar. Just change the extension and, assuming it's not corrupt or anything, it should be fine.
If you needed to, you could extract it to your filesystem and then repackage it as a jar.
1) Rename it to .jar
2) Extract: jar xf filename.jar
3) Repackage: jar cf output.jar input-file(s)
As many other people have pointed out, just extracting the .jar from the .aar file doesn't quite cut it as resources may be missing.
Here are the steps that worked for me (context of Android, your mileage may vary if you have other purposes):
Rename the .aar file to .zip and extract.
The extracted folder is an ADT project that you can import in Eclipse with some minor modifications (see below)!
In the extracted folder rename the contained file classes.jar to whatever you like (in this example myProjectLib.jar) and move it to the lib folder within the extracted folder.
Now in order for Eclipse to accept it you need to put two files into the extracted folder root:
.project
.classpath
To do that, create a new Android dummy project in Eclipse and copy over the files, or copy over from an existing Android project.
Open the .project file and look for the XML name tag and replace the contents of it with myProjectLib (or whatever you called your jar file above) and save.
Now in Eclipse you can File -> New -> Project -> Android Project from existing source.. and point to the extracted folder content.
After import right click on the newly created project, select Properties -> Android, and check Is Library.
In your main project that you want to use the library for, also go to Properties -> Android and add the newly added myProjectLib to the list of dependencies.
For those, who want to do it automatically, I have wrote a little two-lines bash script which does next two things:
Looks for all *.aar files and extracts classes.jar from them
Renames extracted classes.jar to be like the aar but with a new extension
find . -name '*.aar' -exec sh -c 'unzip -d `dirname {}` {} classes.jar' \;
find . -name '*.aar' -exec sh -c 'mv `dirname {}`/classes.jar `echo {} | sed s/aar/jar/g`' \;
That's it!
Android Studio (version: 1.3.2) allows you to seamlessly access the .jar inside a .aar.
Bonus: it automatically decompiles the classes!
Simply follow these steps:
File > New > New Module > Import .JAR/.AAR Package to import you .aar as a module
Add the newly created module as a dependency to your main project (not sure if needed)
Right click on "classes.jar" as shown in the capture below, and click "Show in explorer". Here is your .jar.
Resource based .aar-projects
Finding the classes.jar file inside the .aar file is pretty trivial. However, that approach does not work, if the .aar-project defined some resources (example: R.layout.xyz)
Therefore deaar from CommonsGuy helped me to get a valid
ADT-friendly project out of an .aar-file. In my case I converted
subsampling-scale-image-view. It took me about an hour to set up ruby on my PC.
Another approach is using android-maven-plugin for Eclipse/ADT as
CommonsGuy writes in his blog.
Yet another approach could be, just cloning the whole desired project
as source from git and import it as "Existing Android project"
The 'aar' bundle is the binary distribution of an Android Library Project. .aar file
consists a JAR file and some resource files. You can convert it
as .jar file using this steps
1) Copy the .aar file in a separate folder and Rename the .aar file to .zip file using
any winrar or zip Extractor software.
2) Now you will get a .zip file. Right click on the .zip file and select "Extract files".
Will get a folder which contains "classes.jar, resource, manifest, R.java,
proguard(optional), libs(optional), assets(optional)".
3) Rename the classes.jar file as yourjarfilename.jar and use this in your project.
Note: If you want to get only .jar file from your .aar file use the
above way.
Suppose If you want to include the manifest.xml and resources with your .jar file means
you can just right click on your .aar file and save it as .jar file directly instead of
saving it as a .zip. To view the .jar file which you have extracted, download JD-GUI(Java Decompiler). Then drag and drop your .jar file into this JD_GUI, you can see the .class file in readable formats like a .java file.
If you are using Gradle for your builds - there is a Gradle plugin which allows you to add aar dependency to your java|kotlin|scala|... modules.
https://github.com/stepango/aar2jar
plugins {
id 'java'
id 'com.stepango.aar2jar' version “0.6” // <- this one
}
dependencies {
compileOnlyAar "com.android.support:support-annotations:28.0.0" // <- Use any AAR dependencies
}

Creating an executable installer in Java

I am interested in making a one-click installer for my C# application.
I had the the framework of the application down. The logic of the application in the installer() method was:
public static void installer(){
deleteLegacyFiles(); // deletes old files through a find method
moveSQLite(); // moves the database file
if(checkRevit2013()){ // checks whether Revit '13 is installed
movePlugin2013(); // moves my plugin into the Addin folder or Revit
}else if(checkRevit2014()){ // check whether Revit '14 is installed
movePlugin2014(); // moves my plugin into the Addin folder or Revit
}else{
System.out.println("It does not look like you have either Revit 2013 or Revit 2014 installed.");
}
}
However, this Java script (not Javascript, but a Java script) really only took three folders from the /Desktop/ and copies them to their respective target folders. I am interested in a solution that turns all my three folders into one executable file (something like an .exe or .msi) and do the above actions.
Are there any solutions for this for Java? Something that packages multiple folders/files together and then allows for one-click solutions for installation? I'm not exactly how to phrase what I want, as this is my first software development project. Any suggestions are welcome.
You can create a single executable jar file in java. This jar would have an application that does all the copying you've listed above. But instead of copying from the desktop, it would copy directories that are included in the executable jar. A jar is a zipped file type (in fact you can change the extension from jar to zip and examine the contents).
Your strategy will be to create a regular java application, package as an executable jar. Include the directories you want to install as resources in the jar. Check out the jar documentation for all java utility methods and classes to manipulate jars.
http://docs.oracle.com/javase/tutorial/deployment/jar/
Are you looking for making/building an executable jar file? If so you can use something like one-jar.
http://one-jar.sourceforge.net/index.php?page=introduction&file=intro
Here are the steps:
Create an executable JAR file with your application's CLASS files in it. (Navigate to bin directory of workspace) Name this "main.jar"
jar cfm main.jar manifest.txt *.class OR [jar cfm main.jar manifest.txt .]
Create three directories: MAIN, LIB, and BOOT
Place your "main.jar" file in the MAIN directory.
Place the jar files that your main application depends on in the LIB directory.
Naigate to Packaging- Create a new JAR file out of the MAIN and LIB directories. Name this one "MyUtil.jar". You do not need to add a manifest or do anything special to this file. It does not need to be executable. Just make it so that it contains the contents of the MAIN and LIB directories.
jar cf MyUtil.jar main lib
Extract the contents of the "one-jar-boot.jar" file into the BOOT directory.
Navigate to the BOOT directory, and update the "MyUtil.jar" file with the following:
jar -uvfm ../MyUtil.jar boot-manifest.mf .
Your "MyUtil.jar" file should now be executable. Test it.

Categories