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.
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 made a code that connects to my sqlite driver which is in the CLASSPATH and reads some database file. I want to create an executable which can be used on computers that don't have the sqlite driver.
If I do:
jar cvfe exec.jar main_class
I will get "class not found: org.sqlite.JDBC" when running with
java -jar exec.jar
What should I do to make the executable work?
Edit:
I don't know if it makes any difference, but this is the JDBC driver I use:
https://bitbucket.org/xerial/sqlite-jdbc
You need to include the library inside the JAR. Maybe you don't know this, but JAR files are just ZIP files, so you can change their contents easily. Here are some quick instructions on how to do it. Assuming your JAR file is named exec.jar, and the JAR of the library you want to include (the JAR you downloaded) is driver.jar
Change your file name from exec.jar to exec.zip.
Extract all the contents of exec.zip into folder exec/
Change your library file name from driver.jar to driver.zip
Extract all the contents of driver.zip into folder driver/
Copy the contents of driver/ into exec/, but do not copy the META-INF folder. If a pop-up asks if it's ok to merge the folders, click yes.
Compress all files in exec/ into exec.zip
Rename exec.zip to exec.jar (replace the original).
You can include any java library inside a JAR using this method.
Here is the doc:
C:\Windows\System32>jar /?
Illegal option: /
Usage: jar {ctxui}[vfmn0Me] [jar-file] [manifest-file] [entry-point] [-C dir] files ...
And so I think the command you need is:
jar cvfe exec.jar main_class main_class
I have a java project for which I have used Intellij Idea. The project runs fine, and I have exported it as a jar. The jar export also runs properly, except for one issue:
I have an additional "resources" folder within my project, and there are two csv files (very simple structure with two columns). In the code, I have used their relative paths as follows
private static final __MY_FILE = new File("resources/filename.csv");
As expected, this works properly when I run from the IDE. But when I export the project as a jar, the code crashes with FileNotFoundException because these relative paths are no longer treated as being relative to project folder.
Temporarily, I have resorted to providing the full path in the code (i.e. __MY_FILE = new File("/home/.../resources/filename.csv")), but this is clearly a disastrous practice!
How can I use paths relative to the project folder so that the exported .jar works?
The directory structure is as follows:
networkmeasurements
/matrixmethods
/src
/probabilisticmethods
/src
/utils
/src
/resources
filename.csv
I am trying to get the resource filename.csv from a class inside matrixmethods.
You can't use File, since this file does not exist independently on the file system. Instead you need getResourceAsStream(), for example
...
InputStream in = getClass().getResourceAsStream("/resources/filename.csv");
BufferedReader input = new BufferedReader(new InputStreamReader(in));
...
http://www.jetbrains.com/idea/webhelp/resource-files.html might have good insight. You have to get Intellij to recognise .csv files as property files so it will bundle them.
I have a jar whose content looks as shown below,
Below is my manifest file
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.8.3
Created-By: 1.7.0_06-b24 (Oracle Corporation)
Main-Class: org.sai.com.DerbyDemo
Class-Path: derby.jar derbyclient.jar derbynet.jar derbytools.jar
When i try to run the jar, it has thrown a ClassNotFoundExcception meaning it isn't referencing the jars inside the outer jar.
In the Class-Path attribute, how can I reference jars (derby.jar, etc) inside the actual jar?
You will need a custom class loader for this, have a look at One Jar.
One-JAR lets you package a Java application together with its dependency Jars into a single executable Jar file.
It has an ant task which can simplify the building of it as well.
REFERENCE (from background)
Most developers reasonably assume that putting a dependency Jar file into their own Jar file, and adding a Class-Path attribute to the META-INF/MANIFEST will do the trick:
jarname.jar
| /META-INF
| | MANIFEST.MF
| | Main-Class: com.mydomain.mypackage.Main
| | Class-Path: commons-logging.jar
| /com/mydomain/mypackage
| | Main.class
| commons-logging.jar
Unfortunately this is does not work. The Java Launcher$AppClassLoader does not know how to load classes from a Jar inside a Jar with this kind of Class-Path. Trying to use jar:file:jarname.jar!/commons-logging.jar also leads down a dead-end. This approach will only work if you install (i.e. scatter) the supporting Jar files into the directory where the jarname.jar file is installed.
You can't. From the official tutorial:
By using the Class-Path header in the manifest, you can avoid having
to specify a long -classpath flag when invoking Java to run the your
application.
Note: The Class-Path header points to classes or JAR files on the
local network, not JAR files within the JAR file or classes accessible
over internet protocols. To load classes in JAR files within a JAR
file into the class path, you must write custom code to load those
classes. For example, if MyJar.jar contains another JAR file called
MyUtils.jar, you cannot use the Class-Path header in MyJar.jar's
manifest to load classes in MyUtils.jar into the class path.
In Eclipse you have option to export executable jar.
You have an option to package all project related jars into generated jar and in this way eclipse add custom class loader which will refer to you integrated jars within new jar.
Default implementations of the classloader cannot load from a jar-within-a-jar: in order to do so, the entire 'sub-jar' would have to be loaded into memory, which defeats the random-access benefits of the jar format (reference pending - I'll make an edit once I find the documentation supporting this).
I recommend using a program such as JarSplice to bundle everything for you into one clean executable jar.
Edit: Couldn't find the source reference, but here's an un-resolved RFE off the Sun website describing this exact 'problem': http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4648386
Also, you could 'test' that your program works by placing the library jar files in a \lib sub-directory of your classes directory, then running from the command line. In other words, with the following directory structure:
classes/org/sai/com/DerbyDemo.class
classes/org/sai/com/OtherClassFiles.class
classes/lib/derby.jar
classes/lib/derbyclient.jar
From the command line, navigate to the above-mentioned 'classes' directory, and type:
java -cp .:lib/* org.sai.com.DerbyDemo
if you do not want to create a custom class loader. You can read the jar file stream. And transfer it to a File object. Then you can get the url of the File. Send it to the URLClassLoader, you can load the jar file as you want.
sample:
InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream("example"+ ".jar");
final File tempFile = File.createTempFile("temp", ".jar");
tempFile.deleteOnExit(); // you can delete the temp file or not
try (FileOutputStream out = new FileOutputStream(tempFile)) {
IOUtils.copy(resourceAsStream, out);
}
IOUtils.closeQuietly(resourceAsStream);
URL url = tempFile.toURI().toURL();
URLClassLoader urlClassLoader = new URLClassLoader(new URL[]{url});
urlClassLoader.loadClass()
...
Add the jar files to your library(if using netbeans) and modify your manifest's file classpath as follows:
Class-Path: lib/derby.jar lib/derbyclient.jar lib/derbynet.jar lib/derbytools.jar
a similar answer exists here
in eclipse, right click project, select RunAs -> Run Configuration and save your run configuration, this will be used when you next export as Runnable JARs