Compressing an Extracted Jar File - java

I have extracted a jar file and made some change on it. Now, how can i compress again it as an executable jar file?
Thanks.(Sorry for my bad english.)

Try :
jar cf jar-file input-file(s)
according to http://docs.oracle.com/javase/tutorial/deployment/jar/build.html

By Using Eclipse IDE You can Create your jar file
Below i have one link from which you can learn how to make jar file executable
https://www.cs.utexas.edu/~scottm/cs307/handouts/Eclipse%20Help/jarInEclipse.htm
But For that You have to make project in Eclipse.

You can use this very simple library to pack/unpack jar file programmaticaly
JarManager
Very simple
import java.io.File;
import java.util.List;
import fr.stevecohen.jarmanager.JarUnpacker;
class Test {
JarUnpacker jarUnpacker = new JarUnpacker();
File myfile = new File("./myfile.jar");
File unpackDir = new File("./mydir");
List<File> unpacked_files = jarUnpacker.unpack(myfile.getAbsolutePath(), unpackDir.getAbsolutePath());
}
You can also use maven dependency
<dependency>
<groupId>fr.stevecohen.jarmanager</groupId>
<artifactId>JarManager</artifactId>
<version>0.5.0</version>
</dependency>
Check the last version with the link bellow to use the last dependency
Please use my public issue tracker if you find some bugs

jar cvf filename.jar *
To compress all extracted file to single jar file you can use *
To add single file you also can use filename instead of *
https://docs.oracle.com/javase/tutorial/deployment/jar/appman.html

Related

Can not execute jar of ortools project, Loader.loadNativeLibraries() errors

After mvn install to generate jar. Can not Loader.loadNativeLibraries() on windows.
The version of ortools is 9.0.9048. I used it on windows.
I added the following two jar to the project and I added them as the link said.
Then the two jar is in here of the IDEA.
The pom file is the following:
Then I can run the program normally in IDEA. But when I mvn install to generate the jar file and run the jar by 'java -jar jarfile.jar', it errors as:
It said java.nio.file.NoSuchFileException: /BOOT-INF/lib/ortools-win32-9.0.jar!/win32-x86-64/, but when I open the jar in winrar, it exists.
Does anyone know the reason?
An example of Mac version.
You need two jars when using ortools in Java actually, ortools-java-9.0.9048.jar and ortools-darwin-x86-64-9.0.9048.jar. The two jar is unzipped from the official file and they are in the main directory.
ortools-java-9.0.9048.jar is the algorithm package which you do not need to care to much. Adding dependency to your program is all of you need to do.
The key is the ortools-darwin-x86-64-9.0.9048.jar. The following code is to read this jar to finally call the algorithm in ortools-java-9.0.9048.jar:
import com.google.ortools.Loader;
Loader.loadNativeLibraries();
It usually works well in IDEA. But when you package the code to a jar file, error happens because of Loader.loadNativeLibraries(); can not find the file in the ortools-darwin-x86-64-9.0.9048.jar.
The solution is to unzip ortools-darwin-x86-64-9.0.9048.jar and get the absolute path of libjniortools.dylib(if you are using linux, it will be a file similar as libjniortools.so and a file similar as libjniortools.dll in Windows). And using the following code instead of Loader.loadNativeLibraries();
System.load("Absolute path/libjniortools.dylib");
It will work after you package your code by this method.
The official artifacts are:
groupe: com.google.ortools, artifact: ortools-java
https://search.maven.org/artifact/com.google.ortools/ortools-java/9.0.9048/jar
For macOS, you can try this code, similar like #Muz solution
public static void loadOrToolLibrary() {
String os = System.getProperty("os.name", "generic").toLowerCase(Locale.ENGLISH);
if (os.equals("mac os x")) { // only for MAC local
File file = new File("src/main/resources/macosLocal/libjniortools.dylib");
String absolutePath = file.getAbsolutePath();
System.load(absolutePath);
} else {
Loader.loadNativeLibraries();
}
}

using directories from java jar application

I have java app, where I copy directories (with subdir and files) to SDCard. I wanted to save these dirs with project. In eclipse everything works fine. But, when I export project to runnable jar, app is not able to find or copy these dirs. No error is showed.
File folder2 = new File(getClass().getResource("/urgent/").getFile());
File folder4 = new File(getClass().getResource("/service/").getFile());
The path, I get from jar app, when I want to copy dir is:
jar:file:\C:\path\to\jar\file\myapp.jar!\urgent
project hierarchy:
-project
--src
--build
--urgent
---subdir
--service
---subdir
Is it possible to work with your own directories in jar and how?
You are trying to work on resources on you classpath, a random folder on your harddrive is not in it, so you cannot find it.
What you need to do is:
1. Work with parameters from the commandline (see how to use args[] in your main method)
2. use those arguments to parametrize your custom copy mehod
or:
Try using the copyDirectory(File srcDir, File destDir) method from the Apache Commons IO library instead. To use this reearch how to include libraries in your project, preferably by using maven.

CLOSED - How do I import another class not in Java Library

I have this code in my java project, which reads a file and converts it into a string.
String txt = FileUtils.readFileToString(text);
It uses this class https://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/FileUtils.html
How do I import this into my project?
Thanks :)
If you are using Ant as a build in tool then below solution works,
Step - 1: download .jar file from here,
Step - 2: Then after add it into your class path likewise,
Project right click -> properties
Step 3 : find Jar from you machine, and add it to your class path. likewise,
Click -> OK.
Now, Your problem has been resolved.
First of all you are looking for deprecated method. I suggest you should not use deprecated methods if possible.
Secondly, if you just want to get content of file in String, you can do it in following way with java.nio.file.Files and without using any third party library.
File file = new File("abc.txt");
String txt = new String(Files.readAllBytes(file.toPath()));
Include commons-io jar in build path of your project by downloading it from Apache site -
https://commons.apache.org/proper/commons-io/download_io.cgi
Try following what Rene said here:
Add commons-io dependency to gradle project in Android Studio
You can also try to drag the jar file under Jar folder of lib after downloading it, then right click on the Jar file and select the "Add as library" option. Then select your app from the dialog box.

Add JLayer Library Eclipse

I have a problem with JLayer Library on Eclipse. With JDev it works fine but I have to work in Eclipse for a project and I can't install this Library.
I did Builds Path -> Add Library and import JLayer.zip
As you can see I can't import Javazoom. Thanks for your help
You need to unpack the JLayer zip file to extract the jar file contains the Java class files (jl1.0.1.jar). Use this jar file in the build path as an external jar instead of the zip file.
Also 'import' statements come before the class file definition:
import javazoom.jl.player.AudioDevice;
public class MyClass
{
....
}

Java - Rome rss reader?

I am trying to read rss.
I copied the jar file in to my libs folder, and I added the jar file to my eclipse project as a library.
In order and export i checked my jar file.
Now I am trying to use the rss reader provided by rome
import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.io.SyndFeedInput;
import com.sun.syndication.io.XmlReader;
URL url = new URL("http://myUrlGoesHere");
XmlReader reader = new XmlReader(url);
SyndFeed feed = new SyndFeedInput().build(reader);
I get the following error on the last line:
The type org.jdom.Document cannot be resolved. It is indirectly referenced from required .class file
What does this mean? What is the solution? * no error on imports *
In order to get it to compile I had to use the jdom-1.1.3.jar file from http://www.jdom.org/dist/binary/archive/. After the zip file is extracted it can be found in the directory jdom-1.1.3\jdom\build.
You will need to right click on your project| Select Properties | Java Build Path | Libraries | Add External JARs.
In addition to copying Rome's jar file to your libs folder, you also need to add JDOM's library (jar) to your class path.
The error you are getting says that someone is indirectly referencing JDOM's jar. Probably someone on Rome or any other library you don't control.

Categories

Resources