How do I convert the src.zip file to jar file? - java

Please any one give me the suggestion for this. I'm having the Xerces-J-source.zip file and I need to convert to Xerces-J-source.jar file.

Generally if your ZIP contains the class files (And not the sources):
Just rename the file, a jar is a zip file. But if you're aiming for a class to be launched with command java -jar myProject.jar you should create a MANIFEST file containing the main-class and libraries to use in the classpath.
So in your case since you got the sources (Java file), you'll have to compile classes and create the JAR. Eclipse has an Export as JAR feature if you use it.

That doesn't make any sense. If you need Xerces-J the better is download the jars from the website.
If you want the get the jar form the source, you need to compile it, but there is no need when you can download it.

Related

how to include external jar files while creating a jar file in linux?

I have an external jar file mysql-connector-java-5.1.26 under /Users/remy/Downloads/mysql-connector-java-5.1.26/mysql-connector-java-5.1.26.jar . I want to create a jar file from hello.java which should include the external jar file as specified as well, where hello.java is dependent on the external jar file.
Users
-->remy
-->Downloads
-->mysql-connector-java-5.1.26
-->mysql-connector-java-5.1.26.jar
Users
-->remy
-->Documents
-->Hello.java
How to create the myManifest.txt and include the external jar file so as to compile and run them together?
There are two ways.
Create a MANIFEST.MF that refers to the other jar in its classpath
Whenever you run the program, make sure you include both jars in your classpath.
That said, I think you should go back and read a tutorial because you've made a few mistakes in your question.
"hello.java" is not a jar file as you've said. This is a java source file. It will be compiled into a class file. You can take this class file and put it in a jar file.
You can't name your manifest file "myManifest.txt" AFAIK. It has to be named something specific and be located in a specific place. Read a tutorial to see the details on that.

how to update a external jar with my java class file on eclipse

In my java project, i have added some external jars. I want to update a particular jar with my java class file. Please help me how to do this.
Regards
Rajasekaran.G
If the jar you want to add your class to is a 3r party library, I would recommend against doing what you described (it can lead to subtle surprises and unintended side effects).
If the jar you want to update is your own, I would mode the source for the class into the project that generates that jar and it should be added automatically during the generation of the jar.
If you really want to do it by hand, you can unjar the external jar, add your .class file to the resulting directory structure and jar it up again.
You need to update the jar independently try this,
jar -uvf <existingjar> <class file in appropriate folder structure as per package>
e.g.
jar -uvf current.jar NewClass.class
or if your class is in some package say com.my.util, place your class file in com/my/util/NewClass.class and execute the following from prompt
jar -uvf current.jar com
This will update the current.jar file.
If its an external jar, i wonder if you can even do this? If its your own jar, follow the option given by Attila
try to open your .jar file winrar and place your java class wherever you want on the jar.

Export a java library in a .jar file

I'm still pretty new to java and I'm VERY new to exporting .jar files. I've got a little game that I want to send to some friends and I was told in another question that I should export it to an executable jar file. Well I finally got that working on my computer but when I send it to other people it doesn't work because they don't have the library.
I'm importing the objectdraw library and without that my program won't run at all!
So basically I need to find a way to export the object draw library as part of my .jar file so that they can use it too. Do I simply include it in the included files part of the jar command?
ex: jar cmf MANIFEST.mf Archery.jar * /System/Library/Java/Extensions/objectdraw.jar
or what? I'm working out of the command line right now.
The simplest way is to send the JAR library file too and add a Class-Path entry to the manifest. This entry would look like:
Class-Path: objectdraw.jar
You could also set the CLASSPATH environment variable manually.
Alternatively, you can unpack the library and add all (or just the required files) to your final jar. This doesn't always work though, because some libraries rely on the integrity of teir JAR file.
Finally, it is possible to include the dependency in the main JAR, but it would require a custom class loader.
Turns out the best way I've found to do this is to unpack the library and then put all the resulting files in with your final archive. This way it actually works on other computers.
jar xf library_wanted.jar; jar cvmf MANIFEST.mf end_result.jar *.class library_wanted/

Updating .class file in jar

I want to update a .class file in a jar with a new one. What is the easiest way to do it, especially in the Eclipse IDE?
This tutorial details how to update a jar file
jar -uf jar-file <optional_folder_structure>/input-file(s)
where 'u' means update.
Do you want to do it automatically or manually? If manually, a JAR file is really just a ZIP file, so you should be able to open it with any ZIP reader. (You may need to change the extension first.) If you want to update the JAR file automatically via Eclipse, you may want to look into Ant support in Eclipse and look at the zip task.
Use jar -xvf to extract the files to a directory.
Make your changes and replace the classes.
Use jar -cvf to create a new jar file.
Simply drag and drop your new class file to the JAR using 7-Zip or Winzip. You can even modify a JAR file that is included in a WAR file using the parent folder icon, and click Ok when 7zip detects that the inside file has been modified
Jar is an archive, you can replace a file in it by yourself in your favourite file manager (Total Commander for example).
A JAR file is just a .zip in disguise. The zipped folder contains .class files.
If you're on macOS:
Rename the file to possess the '.zip' extension. e.g. myJar.jar -> myJar.zip.
Decompress the '.zip' (double click on it). A new folder called 'myJar' will appear
Find and replace the .class file with your new .class file.
Select all the contents of the folder 'myJar' and choose 'Compress x items'. DO NOT ZIP THE FOLDER ITSELF, ONLY ITS CONTENTS
Miscellaneous - Compiling a single .class file, with reference to a original jar, on macOS
Make a file myClass.java, containing your code.
Open terminal from Spotlight.
javac -classpath originalJar.jar myClass.java This will create your compiled class called myClass.class.
From here, follow the steps above. You can also use Eclipse to compile it, simply reference the original jar by right clicking on the project, 'Build Path' -> 'Add External Archives'. From here you should be able to compile it as a jar, and use the zip technique above to retrieve the class from the jar.
Editing properties/my_app.properties file inside jar:
"zip -u /var/opt/my-jar-with-dependencies.jar properties/my_app.properties". Basically "zip -u <source> <dest>", where dest is relative to the jar extract folder.
High-level steps:
Setup the environment
Use JD-GUI to peek into the JAR file
Unpack the JAR file
Modify the .class file with a Java Bytecode Editor
Update the modified classes into existing JAR file
Verify it with JD-GUI
Refer below link for detailed steps and methods to do it,
https://www.talksinfo.com/how-to-edit-class-file-from-a-jar/
1) you can extract the file into a folder called
jarname.jar
and then replace the file in the folder, handy if you are updating the class a lot while debugging
2) you can extract the jar replace the file then the jar it up again
3) Open the jar with 7 zip and drag and drop your new class in to copy over the old one
You can find source code of any .jar file online, import the same project in your IDE with basic setups. Make necessary changes in .java file and compile it for .class files.
Once compilation is done You need to extract the jar file, replace the old .class file with new one.
And use below command for reconstruct .jar file
Jar cf test.jar *
Note : I have done so many time this changes in our project, hope you will find it useful.
An alternative is not to replace the .class file in the jar file. Instead put it into a new jar file and ensure that it appears earlier on your classpath than the original jar file.
Not sure I would recommend this for production software but for development it is quick and easy.

Files not extracted from .jar file when run

I have updated my ant build.xml file to include a new file and a new folder. After creating the .jar I check if they exist in the jar by 'unzip\extract', and they are there.
But when executing the .jar neither the folder or the file gets extracted.
Am I missing a step?
Look into getResourceAsStream. It'll keep you from having to extract the files from the jar file. Unless that's your goal.
Your application should be able to use the file directly from within the jar, no need for extracting it. Or do you mean something else?
Are you doing something specific to extract the jar file? I ask because normally jar files are not extracted when executing them.
If you run "java -jar myJar.jar" or "java -cp myJar.jar com.example.MyMainClass" the jar files that is referenced will not be extracted. Java will load your classes and resources directly from the jar file without extracting it.
If you wrap your application up using One-JAR, you can specify an attribute in the Manifest file to extract files that you want (See the One-Jar-Expand manifest attribute).
As a bonus, you will also be able to wrap any dependent libraries along with your code, creating a single distributable jar.

Categories