I am working on creating an Android app and need to import a jar containing a class that I created into it, the class doesn't contain a main method, so, as far as I understand, the jar won't be executable, which is fine. I just need it to be able to be used within the application.
How do I create the jar file?
You can try creating by using command prompt: jar cvf jar-file-name folder-of-classes
Steps to do:
1) Take all the required `.class` files place it into a folder named `xyz`in directory `D:\abc`
2) Open Command prompt and change the current working directory to D:\abc
3) run command as jar cvf test.jar xyz
check the directory D:\abc there will be a jar file named test.jar containing the folder with all the class files.
Hope it helps
jar file is just a bunch of .class file. select your android package Right click -> New -> Java Class.
copy your old class file code into newly created. Do care class name & .class file be same name.
if you mean to import java .jar dependency
Select your app. Right click goto New->Module->import .jar/.aar package
Using the Jar file in your application
In your applications Android.mk; specify the name of the JAR file under the LOCAL_JAVA_LIBRARIES :=
Related
I need to replace a single class file from a jar file. The jar file is quite big and every time I don't want to extract it and replace manually. I want to automate this procedure. Can someone please help me on this.
When jar file is extracted below folders are created :
render
classes
com
I need to go inside "com/cgp/f1/cmmi/" folder and replace a class file inside it.
Things I tried :
zip file.jar com/cgp/f1/cmmi/services.class Services.class
jar uf file.jar com/cgp/f1/cmmi/ services.class
jar -uf file.jar com\cgp\f1\cmmi\ services.class
jar uf file.jar com/cgp/f1/cmmi/services.class services.class
The error I am getting is :
when using jar command
com\cgp\f1\cmmi\ : no such file or directory
when using zip command :
zip warning: name not matched: com\cgp\f1\cmmi\Services.class
can some one please guide me where I am going wrong.
Maybe the jar -uf found here could help you: How to update one file in a zip archive
If graphical apps are an option, you could use winrar or 7-zip to replace the class. You don't need to extract the jar file to make this work. Just open the jar with one of those apps, go to de directory where is the class file to be replaced, drag-and-drop the new file to replace the old one and save.
I made a java program with a properties file named config.properties.
It works perfectly on Eclipse.
I'm trying to create an executable jar for this programe.
Using the classic method (right click on project, export, executable jar file...) i get a working jar but when i try to edit my config.properties file the changes are not taken in account for the following execution of my jar.
How can I get, on the one hand an executable jar and on the other hand a config.properties file (outside of my Jar) that can be edited by the users to change the parameters of my Jar code ?
Currently my property file is stocked in /src and declared like this :
public static ResourceBundle bundle = ResourceBundle.getBundle("config");
When I need to use one of the properties of this file in my java code I use :
bundle.getString("Car.Color");
Thanks for your help :)
Edit your classpath to include a directory within which your properties file is located. For example:
java -classpath C:\java\MyClasses com.myapp.RunIt
I have made a Java Swing application and now I want to export it as an executable jar file. I have created the app in Eclipse, and it has the following structure :
where folder mysqlconnector contains also a jar file. So first I tried following the instructions in this link and created seo.jar, but when I try to execute it from the terminal by java -jar seo.jar I get an error :
Error: Could not find file connectionprops.properties
As the screenshot shows, I tried putting connectionprops.properties in main package, but the same problem remains.
Then I tried making a manifest file named manifest.mf with contents :
Main-Class: bin.main.MainClass //also tried Main-Class: MainClass
as the structure of my project is :
seo --> bin --> main --> MainClass.class
I placed the manifest.mf in folder seo and I gave the following command in the terminal :
jar -cvfm seo.jar manifest.mf *
but again when executing it from the terminal by java -jar seo.jar I get an error :
Error: Could not find or load main class bin.main.MainClass
What am I doing wrong? Should I change something in my project structure? Is there a problem that I have other jar files inside my project? How can I create the executable jar and execute it successfully?
The Manifest Main-Class attribute needs to receive the package path to your Main class. It doesn't matter what the structure of the project looks like, what matters is the fully qualified package name that you have inside the src/ folder. So in this case, main.MainClass is what you should write there.
Also, if you receive other errors when trying to read the connectionprops.properties in your program, try to open the file using
someObject.getClass().getResourceAsStream("connectionprops.properties")
bin.main is not a package name. You can't add this to manifest.mf.
You should add the package name only if you have the main class in the package. If your package is default then put only the main class.
Main-Class: MainClass
I have a jar file in C:\ (xx.jar). I need to replace a class file (path: com\sample\folder\xfile.class) within xx.jar with a class file (yfile.class) that is placed C:\.
Please help me with the command for replacing one class file in the jar with another class file outside the jar. I am struggling in pointing the directory structure.
I tried with,
jar uf C:\xx.jar C:\yfile.class
The above command creates a new folder C:\ within xx.jar and the yfile.class comes inside C:\. But I want the file to be inside the com.sample.folder in the xx.jar
Kindly help. Thanks in advance!
You can't, really. Jar is really meant to package a prepared folder.
I guess this is also why maven etc create a build folder with everything that is going to end up in the jar/war/ear file.
jar uf C:\xx.jar C:\yfile.class
is indeed just adding the file to the archive.
In the folder you are archiving from you can use some -C directive, but that is about it. Suppose yout myfile.class is in a dir called C:\classes and you cal jar from that folder
jar uf C:\xx.jar -C .. classes\file.class
Is going to add the folder classes to the jar with the file myclass.class in it.
Kind regards
I'm trying to create my first jar file and I'm having trouble. I'm using the DOS to do it and my path and everything are all set up correctly.
These are the files I'm trying to include:
Bot.class
Start.class
Stop.class
Thread.class
I've created a manifest file that looks like this (it also has a carriage return at the end)
Main-Class: Bot
Here is what I'm running through the DOS
jar cf Bot.jar Manifest.txt Bot.class Start.class Stop.class Visit.class
It creates the jar successfully and but when I try and execute it, either by clicking on it or using the command prompt I get
Could not find the main class: Bot. Program will exit.
I have tried using both Bot and Bot.class in the manifest file but still get the same error. I've only tried changing the extension on the manifest file to .mf instead of .txt
Thanks in advance for your help!
jar cmf Manifest.txt Bot.jar Bot.class Start.class Stop.class Visit.class
The m switch is needed to tell jar which file is the manifest. Note that the order of m and f must match the order of Manifest.txt and bot.jar
Remember that the directory structure in the jar must match your package structure.
e.g. for package com.me.Bot the added file should be com/me/Bot.class not just Bot.class
learning how to manually build a jar is a fun thing to do once. for the long term, however, i'd recommend learning a build tool (ant, maven, whatever).