How can I reflectively get all of the packages in the project? I started with Package.getPackages(), but that only got all the packages associated to the current package. Is there a way to do this?
#PhilippWendler's comment led me to a method of accomplishing what I needed. I tweaked the method a little to make it recursive.
/**
* Recursively fetches a list of all the classes in a given
* directory (and sub-directories) that have the #UnitTestable
* annotation.
* #param packageName The top level package to search.
* #param loader The class loader to use. May be null; we'll
* just grab the current threads.
* #return The list of all #UnitTestable classes.
*/
public List<Class<?>> getTestableClasses(String packageName, ClassLoader loader) {
// State what package we are exploring
System.out.println("Exploring package: " + packageName);
// Create the list that will hold the testable classes
List<Class<?>> ret = new ArrayList<Class<?>>();
// Create the list of immediately accessible directories
List<File> directories = new ArrayList<File>();
// If we don't have a class loader, get one.
if (loader == null)
loader = Thread.currentThread().getContextClassLoader();
// Convert the package path to file path
String path = packageName.replace('.', '/');
// Try to get all of nested directories.
try {
// Get all of the resources for the given path
Enumeration<URL> res = loader.getResources(path);
// While we have directories to look at, recursively
// get all their classes.
while (res.hasMoreElements()) {
// Get the file path the the directory
String dirPath = URLDecoder.decode(res.nextElement()
.getPath(), "UTF-8");
// Make a file handler for easy managing
File dir = new File(dirPath);
// Check every file in the directory, if it's a
// directory, recursively add its viable files
for (File file : dir.listFiles()) {
if (file.isDirectory())
ret.addAll(getTestableClasses(packageName + '.' + file.getName(), loader));
}
}
} catch (IOException e) {
// We failed to get any nested directories. State
// so and continue; this directory may still have
// some UnitTestable classes.
System.out.println("Failed to load resources for [" + packageName + ']');
}
// We need access to our directory, so we can pull
// all the classes.
URL tmp = loader.getResource(path);
System.out.println(tmp);
if (tmp == null)
return ret;
File currDir = new File(tmp.getPath());
// Now we iterate through all of the classes we find
for (String classFile : currDir.list()) {
// Ensure that we only find class files; can't load gif's!
if (classFile.endsWith(".class")) {
// Attempt to load the class or state the issue
try {
// Try loading the class
Class<?> add = Class.forName(packageName + '.' +
classFile.substring(0, classFile.length() - 6));
// If the class has the correct annotation, add it
if (add.isAnnotationPresent(UnitTestable.class))
ret.add(add);
else
System.out.println(add.getName() + " is not a UnitTestable class");
} catch (NoClassDefFoundError e) {
// The class loader could not load the class
System.out.println("We have found class [" + classFile + "], and couldn't load it.");
} catch (ClassNotFoundException e) {
// We couldn't even find the damn class
System.out.println("We could not find class [" + classFile + ']');
}
}
}
return ret;
}
It's possible but tricky and expensive, since you need to walk the classpath yourself. Here is how TestNG does it, you can probably extract the important part for yourself:
https://github.com/cbeust/testng/blob/master/src/main/java/org/testng/internal/PackageUtils.java
This approach prints all packages only (at least a root "packageName" has to be given first).
It is derived from above.
package devTools;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
public class DevToolUtil {
/**
* Method prints all packages (at least a root "packageName" has to be given first).
*
* #see http://stackoverflow.com/questions/9316726/reflectively-get-all-packages-in-a-project
* #since 2016-12-05
* #param packageName
* #param loader
* #return List of classes.
*/
public List<Class<?>> getTestableClasses(final String packageName, ClassLoader loader) {
System.out.println("Exploring package: " + packageName);
final List<Class<?>> ret = new ArrayList<Class<?>>();
if (loader == null) {
loader = Thread.currentThread().getContextClassLoader();
}
final String path = packageName.replace('.', '/');
try {
final Enumeration<URL> res = loader.getResources(path);
while (res.hasMoreElements()) {
final String dirPath = URLDecoder.decode(res.nextElement().getPath(), "UTF-8");
final File dir = new File(dirPath);
if (dir.listFiles() != null) {
for (final File file : dir.listFiles()) {
if (file.isDirectory()) {
final String packageNameAndFile = packageName + '.' + file.getName();
ret.addAll(getTestableClasses(packageNameAndFile, loader));
}
}
}
}
} catch (final IOException e) {
System.out.println("Failed to load resources for [" + packageName + ']');
}
return ret;
}
public static void main(final String[] args) {
new DevToolUtil().getTestableClasses("at", null);
}
}
May be off-topic (because it is not exactly in terms of java "reflection") ...
However, how about the following solution:
Java packages can be treated as folders (or directories on Linux\UNIX).
Assuming that you have a root package and its absolute path is known, you can recursively print all sub-folders that have *.java or *.class files using the following batch as the basis:
#echo off
rem List all the subfolders under current dir
FOR /R "." %%G in (.) DO (
Pushd %%G
Echo now in %%G
Popd )
Echo "back home"
You can wrap this batch in java or if you run on Linux\Unix rewrite it in some shell script.
Good Luck!
Aviad.
Related
How should I effectively check for the availability of particular folder(myfolder) recursively and if available, then create a tmp directory parallel to it
Example:
#ls -l
--parent folder
--projects
-- sub folders (further depth is possible)
-- myfolder
-- tmp
I'm from python background and yet to get used to java. Below is what I could come up with.
import java.io.File;
class Main
{
public static void main(String[] args)
{
String currentDir = System.getProperty("user.dir");
String projectDir = currentDir + "/projects"; // under this I have to search the for the `myfolder` recursively.
File file = new File(projectDir);
if (file.isDirectory()) {
new File("tmp").mkdirs();
}
else {
System.out.println("Directory doesn't exist!!");
}
}
note: I use java 8
Below is a method that recursively searches through all the sub-directories that might be contained within the provided local directory path for a specific directory (folder) name. When the first instance of that directory name is found the search halts and the full path to that directory is returned.
From that point, the returned path string should be parsed to get the parent path. Something like that could be done something like this:
String foundFolderParentPath = foundDirectory.substring(0,
foundDirectory.lastIndexOf(File.separator));
Now you would want to check and see if the tmp directory already exists there. Maybe you don't need to create it or, you may want to carry out some other action based on that fact:
if (new File(foundFolderParentPath + File.separator + "tmp").exists()) {
// tmp already exists...Do whatever...
}
else {
// Otherwise Create the tmp directory...
new File(foundFolderParentPath + File.separator + "tmp").mkdir();
}
Here is the recursive findDirectory() method:
/**
* This method recursively searches through all sub-directories beginning
* from the supplied searchStartPath until the supplied folder to find is
* found.<br>
*
* #param searchStartPath (String) The full path to start the search from.<br>
*
* #param folderToSearchFor (String) The directory (folder) or sub-directory
* (sub-folder) name to search for. Just a single name should be supplied, not multiple directory names..<br>
*
* #return (String) If the search is successful then the full path to that
* found folder is returned. If the search was unsuccessful then Null String
* (""), an empty string is returned.
*/
public static String findDirectory(String searchStartPath, String folderToSearchFor) {
String foundPath = "";
File[] folders = new File(searchStartPath).listFiles(File::isDirectory);
if (folders.length == 0) {
return "";
}
String tmp;
for (int i = 0; i < folders.length; i++) {
String currentPath = folders[i].getAbsolutePath();
if (currentPath.equals(folderToSearchFor) ||
currentPath.substring(currentPath.lastIndexOf(File.separator) + 1)
.equals(folderToSearchFor)) {
foundPath = currentPath;
break;
}
tmp = "";
// The recursive call...
tmp = findDirectory(folders[i].getAbsolutePath(), folderToSearchFor);
if (!tmp.isEmpty()) {
// Directory is found...
foundPath = tmp;
break; // Get out of loop. It's No longer needed.
}
}
return foundPath;
}
How you might use this method:
String currentDir = System.getProperty("user.dir");
String projectDir = currentDir + "/Projects"; // under this I have to search the for the `myfolder` recursively.
String searchForDirectory = "mySpecialFolder";
String foundDirectory = findDirectory(currentDir, searchForDirectory);
if (foundDirectory.isEmpty()) {
System.out.println("The folder to find (" + searchForDirectory
+ ") could not be found!");
}
else {
System.out.println("The ' " + searchForDirectory +
"' Folder is found at: --> " + foundDirectory);
/* Create the 'tmp' folder within the same parent folder where
mySpecialFolder resides in. */
String foundFolderParentPath = foundDirectory.substring(0,
foundDirectory.lastIndexOf(File.separator));
// Is there a 'tmp' folder already there?
if (new File(foundFolderParentPath + File.separator + "tmp").exists()) {
// Yes there is..
System.out.println("\nThere is no need to create the 'tmp' folder! It already"
+ "exists within the\nparent path of: --> " + foundFolderParentPath);
}
else {
// No here isn't so create it...
new File(foundFolderParentPath + File.separator + "tmp").mkdir();
System.out.println("The 'tmp' folder was created within the parent path indicated below:");
System.out.println(foundFolderParentPath);
System.out.println();
}
// Display a File-Chooser to prove it just for the heck of it.
javax.swing.JFileChooser fc = new javax.swing.JFileChooser(foundFolderParentPath);
fc.showDialog(null, "Just A Test");
}
I'm trying to access an XML file within a jar file, from a separate jar that's running as a desktop application. I can get the URL to the file I need, but when I pass that to a FileReader (as a String) I get a FileNotFoundException saying "The file name, directory name, or volume label syntax is incorrect."
As a point of reference, I have no trouble reading image resources from the same jar, passing the URL to an ImageIcon constructor. This seems to indicate that the method I'm using to get the URL is correct.
URL url = getClass().getResource("/xxx/xxx/xxx/services.xml");
ServicesLoader jsl = new ServicesLoader( url.toString() );
Inside the ServicesLoader class I have
XMLReader xr = XMLReaderFactory.createXMLReader();
xr.setContentHandler( this );
xr.setErrorHandler( this );
xr.parse( new InputSource( new FileReader( filename )));
What's wrong with using this technique to read the XML file?
Looks like you want to use java.lang.Class.getResourceAsStream(String), see
https://docs.oracle.com/javase/8/docs/api/java/lang/Class.html#getResourceAsStream-java.lang.String-
You don't say if this is a desktop or web app. I would use the getResourceAsStream() method from an appropriate ClassLoader if it's a desktop or the Context if it's a web app.
It looks as if you are using the URL.toString result as the argument to the FileReader constructor. URL.toString is a bit broken, and instead you should generally use url.toURI().toString(). In any case, the string is not a file path.
Instead, you should either:
Pass the URL to ServicesLoader and let it call openStream or similar.
Use Class.getResourceAsStream and just pass the stream over, possibly inside an InputSource. (Remember to check for nulls as the API is a bit messy.)
The problem was that I was going a step too far in calling the parse method of XMLReader. The parse method accepts an InputSource, so there was no reason to even use a FileReader. Changing the last line of the code above to
xr.parse( new InputSource( filename ));
works just fine.
I'd like to point out that one issues is what if the same resources are in multiple jar files.
Let's say you want to read /org/node/foo.txt, but not from one file, but from each and every jar file.
I have run into this same issue several times before.
I was hoping in JDK 7 that someone would write a classpath filesystem, but alas not yet.
Spring has the Resource class which allows you to load classpath resources quite nicely.
I wrote a little prototype to solve this very problem of reading resources form multiple jar files. The prototype does not handle every edge case, but it does handle looking for resources in directories that are in the jar files.
I have used Stack Overflow for quite sometime. This is the second answer that I remember answering a question so forgive me if I go too long (it is my nature).
This is a prototype resource reader. The prototype is devoid of robust error checking.
I have two prototype jar files that I have setup.
<pre>
<dependency>
<groupId>invoke</groupId>
<artifactId>invoke</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>node</groupId>
<artifactId>node</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
The jar files each have a file under /org/node/ called resource.txt.
This is just a prototype of what a handler would look like with classpath://
I also have a resource.foo.txt in my local resources for this project.
It picks them all up and prints them out.
package com.foo;
import java.io.File;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URI;
import java.net.URL;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
/**
* Prototype resource reader.
* This prototype is devoid of error checking.
*
*
* I have two prototype jar files that I have setup.
* <pre>
* <dependency>
* <groupId>invoke</groupId>
* <artifactId>invoke</artifactId>
* <version>1.0-SNAPSHOT</version>
* </dependency>
*
* <dependency>
* <groupId>node</groupId>
* <artifactId>node</artifactId>
* <version>1.0-SNAPSHOT</version>
* </dependency>
* </pre>
* The jar files each have a file under /org/node/ called resource.txt.
* <br />
* This is just a prototype of what a handler would look like with classpath://
* I also have a resource.foo.txt in my local resources for this project.
* <br />
*/
public class ClasspathReader {
public static void main(String[] args) throws Exception {
/* This project includes two jar files that each have a resource located
in /org/node/ called resource.txt.
*/
/*
Name space is just a device I am using to see if a file in a dir
starts with a name space. Think of namespace like a file extension
but it is the start of the file not the end.
*/
String namespace = "resource";
//someResource is classpath.
String someResource = args.length > 0 ? args[0] :
//"classpath:///org/node/resource.txt"; It works with files
"classpath:///org/node/"; //It also works with directories
URI someResourceURI = URI.create(someResource);
System.out.println("URI of resource = " + someResourceURI);
someResource = someResourceURI.getPath();
System.out.println("PATH of resource =" + someResource);
boolean isDir = !someResource.endsWith(".txt");
/** Classpath resource can never really start with a starting slash.
* Logically they do, but in reality you have to strip it.
* This is a known behavior of classpath resources.
* It works with a slash unless the resource is in a jar file.
* Bottom line, by stripping it, it always works.
*/
if (someResource.startsWith("/")) {
someResource = someResource.substring(1);
}
/* Use the ClassLoader to lookup all resources that have this name.
Look for all resources that match the location we are looking for. */
Enumeration resources = null;
/* Check the context classloader first. Always use this if available. */
try {
resources =
Thread.currentThread().getContextClassLoader().getResources(someResource);
} catch (Exception ex) {
ex.printStackTrace();
}
if (resources == null || !resources.hasMoreElements()) {
resources = ClasspathReader.class.getClassLoader().getResources(someResource);
}
//Now iterate over the URLs of the resources from the classpath
while (resources.hasMoreElements()) {
URL resource = resources.nextElement();
/* if the resource is a file, it just means that we can use normal mechanism
to scan the directory.
*/
if (resource.getProtocol().equals("file")) {
//if it is a file then we can handle it the normal way.
handleFile(resource, namespace);
continue;
}
System.out.println("Resource " + resource);
/*
Split up the string that looks like this:
jar:file:/Users/rick/.m2/repository/invoke/invoke/1.0-SNAPSHOT/invoke-1.0-SNAPSHOT.jar!/org/node/
into
this /Users/rick/.m2/repository/invoke/invoke/1.0-SNAPSHOT/invoke-1.0-SNAPSHOT.jar
and this
/org/node/
*/
String[] split = resource.toString().split(":");
String[] split2 = split[2].split("!");
String zipFileName = split2[0];
String sresource = split2[1];
System.out.printf("After split zip file name = %s," +
" \nresource in zip %s \n", zipFileName, sresource);
/* Open up the zip file. */
ZipFile zipFile = new ZipFile(zipFileName);
/* Iterate through the entries. */
Enumeration entries = zipFile.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
/* If it is a directory, then skip it. */
if (entry.isDirectory()) {
continue;
}
String entryName = entry.getName();
System.out.printf("zip entry name %s \n", entryName);
/* If it does not start with our someResource String
then it is not our resource so continue.
*/
if (!entryName.startsWith(someResource)) {
continue;
}
/* the fileName part from the entry name.
* where /foo/bar/foo/bee/bar.txt, bar.txt is the file
*/
String fileName = entryName.substring(entryName.lastIndexOf("/") + 1);
System.out.printf("fileName %s \n", fileName);
/* See if the file starts with our namespace and ends with our extension.
*/
if (fileName.startsWith(namespace) && fileName.endsWith(".txt")) {
/* If you found the file, print out
the contents fo the file to System.out.*/
try (Reader reader = new InputStreamReader(zipFile.getInputStream(entry))) {
StringBuilder builder = new StringBuilder();
int ch = 0;
while ((ch = reader.read()) != -1) {
builder.append((char) ch);
}
System.out.printf("zip fileName = %s\n\n####\n contents of file %s\n###\n", entryName, builder);
} catch (Exception ex) {
ex.printStackTrace();
}
}
//use the entry to see if it's the file '1.txt'
//Read from the byte using file.getInputStream(entry)
}
}
}
/**
* The file was on the file system not a zip file,
* this is here for completeness for this example.
* otherwise.
*
* #param resource
* #param namespace
* #throws Exception
*/
private static void handleFile(URL resource, String namespace) throws Exception {
System.out.println("Handle this resource as a file " + resource);
URI uri = resource.toURI();
File file = new File(uri.getPath());
if (file.isDirectory()) {
for (File childFile : file.listFiles()) {
if (childFile.isDirectory()) {
continue;
}
String fileName = childFile.getName();
if (fileName.startsWith(namespace) && fileName.endsWith("txt")) {
try (FileReader reader = new FileReader(childFile)) {
StringBuilder builder = new StringBuilder();
int ch = 0;
while ((ch = reader.read()) != -1) {
builder.append((char) ch);
}
System.out.printf("fileName = %s\n\n####\n contents of file %s\n###\n", childFile, builder);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
} else {
String fileName = file.getName();
if (fileName.startsWith(namespace) && fileName.endsWith("txt")) {
try (FileReader reader = new FileReader(file)) {
StringBuilder builder = new StringBuilder();
int ch = 0;
while ((ch = reader.read()) != -1) {
builder.append((char) ch);
}
System.out.printf("fileName = %s\n\n####\n contents of file %s\n###\n", fileName, builder);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
}
}
You can see a fuller example here with the sample output.
Here's a sample code on how to read a file properly inside a jar file (in this case, the current executing jar file)
Just change executable with the path of your jar file if it is not the current running one.
Then change the filePath to the path of the file you want to use inside the jar file. I.E. if your file is in
someJar.jar\img\test.gif
. Set the filePath to "img\test.gif"
File executable = new File(BrowserViewControl.class.getProtectionDomain().getCodeSource().getLocation().toURI());
JarFile jar = new JarFile(executable);
InputStream fileInputStreamReader = jar.getInputStream(jar.getJarEntry(filePath));
byte[] bytes = new byte[fileInputStreamReader.available()];
int sizeOrig = fileInputStreamReader.available();
int size = fileInputStreamReader.available();
int offset = 0;
while (size != 0){
fileInputStreamReader.read(bytes, offset, size);
offset = sizeOrig - fileInputStreamReader.available();
size = fileInputStreamReader.available();
}
Outside of your technique, why not use the standard Java JarFile class to get the references you want? From there most of your problems should go away.
If you use resources extensively, you might consider using
Commons VFS.
Also supports:
* Local Files
* FTP, SFTP
* HTTP and HTTPS
* Temporary Files "normal FS backed)
* Zip, Jar and Tar (uncompressed, tgz or tbz2)
* gzip and bzip2
* resources
* ram - "ramdrive"
* mime
There's also JBoss VFS - but it's not much documented.
I have 2 CSV files that I use to read data. The java program is exported as a runnable jar file. When you export it, you will figure out it doesn't export your resources with it.
I added a folder under project called data in eclipse. In that folder i stored my csv files.
When I need to reference those files I do it like this...
private static final String ZIP_FILE_LOCATION_PRIMARY = "free-zipcode-database-Primary.csv";
private static final String ZIP_FILE_LOCATION = "free-zipcode-database.csv";
private static String getFileLocation(){
String loc = new File("").getAbsolutePath() + File.separatorChar +
"data" + File.separatorChar;
if (usePrimaryZipCodesOnly()){
loc = loc.concat(ZIP_FILE_LOCATION_PRIMARY);
} else {
loc = loc.concat(ZIP_FILE_LOCATION);
}
return loc;
}
Then when you put the jar in a location so it can be ran via commandline, make sure that you add the data folder with the resources into the same location as the jar file.
I'm writing a short application that opens a .jar file, runs a few transforms on its contents, then wraps it up as a .jar file again. I'm using the approach from How to use JarOutputStream to create a JAR file? to create the new jar file and it's working with the exception that it's creating a file hierarchy in new jar file that stretches all the way back to my computer's root.
It's perfectly logical that this is happening since I'm passing the process the target directory as a path from the computer root. What I can't find is any other way of communicating the context that process needs to be able to find my target folder. How do I set the context from which I want the .jar file to be created?
To clarify:
I've rewritten the Run method of the solution linked above to accept two parameters: a string defining the name and location of the output jar file and a string defining the location of the folder I want compressed, like so:
public void run(String output, String inputDirectory) throws IOException
{
JarOutputStream target = new JarOutputStream(new FileOutputStream(output));
add(new File(inputDirectory), target, inputDirectory.length());
target.close();
}
Two sample values that I hand off to the method would be: C:/temp/964ca469-5f7b-4c56-8b5a-72b4c1c851e0/help.jar and C:/temp/964ca469-5f7b-4c56-8b5a-72b4c1c851e0/out/
I want the structure of my .jar file to have its root at the forward slash following "out", but instead the .jar file's hierarchy is:
C:
|-Temp
|-964ca469-5f7b-4c56-8b5a-72b4c1c851e0
|-out
|-{content}
I've tried passing the length of the string preceding the actual content to the Add method and paring it off before adding the JarEntry, but that just gets me an out of index error, which makes perfect sense because I'm just frikkin' groping.
There must be a way of setting the JarEntry class to a specific point in a folder hierarchy before adding a file, or some other means of doing the same thing, but I canna find it so far.
Thanks.
There is no 'root context' in a JAR file. What the files and paths are is completely dependent on what name(s) you put into the JarEntries.
Never figured out how to do it with Jar file creation, but as fge suggested the zip filesystem provider was indeed easier to work with. I used this in my main method:
CreateJarPackage zipper = new CreateJarPackage();
System.out.println(baseline + unique_directory + "/" + out_jar_file);
System.out.println(baseline + unique_directory + "/out/");
System.out.println(baseline.length() + unique_directory.length() + 5);
try {
zipper.addZipFiles(baseline + unique_directory + "/" + out_jar_file, baseline + unique_directory + "/out/", baseline.length() + unique_directory.length() + 5);
} catch (Throwable e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
File folder = new File(baseline + unique_directory + "/out/");
File[] listOfFiles = folder.listFiles();
for (int i = 0; i < listOfFiles.length; i++) {
if (listOfFiles[i].isDirectory()) {
try {
zipper.addZipFiles(baseline + unique_directory + "/" + out_jar_file, listOfFiles[i].toString(), baseline.length() + unique_directory.length() + 5);
} catch (Throwable e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
To call this:
import java.io.File;
import java.net.URI;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.HashMap;
import java.util.Map;
public class FinalZipCreator {
public static void processList(URI uri, Map<String, String> env, String path)
throws Throwable {
try (FileSystem zipfs = FileSystems.newFileSystem(uri, env)) {
File folder = new File(path);
File[] listOfFiles = folder.listFiles();
int index = path.length();
for (int i = 0; i < listOfFiles.length; i++) {
if (listOfFiles[i].isFile()) {
Path externalTxtFile =Paths.get(listOfFiles[i].toString());
Path pathInZipfile = zipfs.getPath(listOfFiles[i]
.toString().substring(index));
// copy a file into the zip file
Files.copy(externalTxtFile, pathInZipfile,
StandardCopyOption.REPLACE_EXISTING);
} else if (listOfFiles[i].isDirectory()) {
Path externalTxtFile = Paths.get(listOfFiles[i].toString());
Path pathInZipfile = zipfs.getPath(listOfFiles[i]
.toString().substring(index));
// copy a file into the zip file
Files.copy(externalTxtFile, pathInZipfile,
StandardCopyOption.REPLACE_EXISTING);
File folder2 = new File(listOfFiles[i].toString());
File[] listOfFiles2 = folder2.listFiles();
int index2 = listOfFiles[i].toString().length();
for (int e = 0; e < listOfFiles2.length; e++) {
if (listOfFiles2[i].isFile()) {
Path externalTxtFile2 = Paths.get(listOfFiles2[e].toString());
Path pathInZipfile2 = zipfs.getPath(listOfFiles2[e]
.toString().substring(index2));
// copy a file into the zip file
Files.copy(externalTxtFile2, pathInZipfile2,
StandardCopyOption.REPLACE_EXISTING);
}
}
}
}}
}
}
There's likely a zillion better ways of doing it, but it worked. Thanks for the help.
As a side note, it's the equivalent of the "pathInZipFile" function that I could never locate in the Jar creator.
I am not in front of an IDE right now, just looking at the API specs.
CodeSource src = MyClass.class.getProtectionDomain().getCodeSource();
if (src != null) {
URL jar = src.getLocation();
}
I want to determine which JAR file a class is from. Is this the way to do it?
Yes. It works for all classes except classes loaded by bootstrap classloader. The other way to determine is:
Class klass = String.class;
URL location = klass.getResource('/' + klass.getName().replace('.', '/') + ".class");
As notnoop pointed out klass.getResource() method returns the location of the class file itself. For example:
jar:file:/jdk/jre/lib/rt.jar!/java/lang/String.class
file:/projects/classes/pkg/MyClass$1.class
The getProtectionDomain().getCodeSource().getLocation() method returns the location of the jar file or CLASSPATH
file:/Users/home/java/libs/ejb3-persistence-1.0.2.GA.jar
file:/projects/classes
Checkout the LiveInjector.findPathJar() from Lombok Patcher LiveInjector.java. Note that it special cases where the file doesn't actually live in a jar, and you might want to change that.
/**
* If the provided class has been loaded from a jar file that is on the local file system, will find the absolute path to that jar file.
*
* #param context The jar file that contained the class file that represents this class will be found. Specify {#code null} to let {#code LiveInjector}
* find its own jar.
* #throws IllegalStateException If the specified class was loaded from a directory or in some other way (such as via HTTP, from a database, or some
* other custom classloading device).
*/
public static String findPathJar(Class<?> context) throws IllegalStateException {
if (context == null) context = LiveInjector.class;
String rawName = context.getName();
String classFileName;
/* rawName is something like package.name.ContainingClass$ClassName. We need to turn this into ContainingClass$ClassName.class. */ {
int idx = rawName.lastIndexOf('.');
classFileName = (idx == -1 ? rawName : rawName.substring(idx+1)) + ".class";
}
String uri = context.getResource(classFileName).toString();
if (uri.startsWith("file:")) throw new IllegalStateException("This class has been loaded from a directory and not from a jar file.");
if (!uri.startsWith("jar:file:")) {
int idx = uri.indexOf(':');
String protocol = idx == -1 ? "(unknown)" : uri.substring(0, idx);
throw new IllegalStateException("This class has been loaded remotely via the " + protocol +
" protocol. Only loading from a jar on the local file system is supported.");
}
int idx = uri.indexOf('!');
//As far as I know, the if statement below can't ever trigger, so it's more of a sanity check thing.
if (idx == -1) throw new IllegalStateException("You appear to have loaded this class from a local jar file, but I can't make sense of the URL!");
try {
String fileName = URLDecoder.decode(uri.substring("jar:file:".length(), idx), Charset.defaultCharset().name());
return new File(fileName).getAbsolutePath();
} catch (UnsupportedEncodingException e) {
throw new InternalError("default charset doesn't exist. Your VM is borked.");
}
}
Use
String path = <Any of your class within the jar>.class.getProtectionDomain().getCodeSource().getLocation().getPath();
If this contains multiple entries then do some substring operation.
private String resourceLookup(String lookupResourceName) {
try {
if (lookupResourceName == null || lookupResourceName.length()==0) {
return "";
}
// "/java/lang/String.class"
// Check if entered data was in java class name format
if (lookupResourceName.indexOf("/")==-1) {
lookupResourceName = lookupResourceName.replaceAll("[.]", "/");
lookupResourceName = "/" + lookupResourceName + ".class";
}
URL url = this.getClass().getResource(lookupResourceName);
if (url == null) {
return("Unable to locate resource "+ lookupResourceName);
}
String resourceUrl = url.toExternalForm();
Pattern pattern =
Pattern.compile("(zip:|jar:file:/)(.*)!/(.*)", Pattern.CASE_INSENSITIVE);
String jarFilename = null;
String resourceFilename = null;
Matcher m = pattern.matcher(resourceUrl);
if (m.find()) {
jarFilename = m.group(2);
resourceFilename = m.group(3);
} else {
return "Unable to parse URL: "+ resourceUrl;
}
if (!jarFilename.startsWith("C:") ){
jarFilename = "/"+jarFilename; // make absolute path on Linux
}
File file = new File(jarFilename);
Long jarSize=null;
Date jarDate=null;
Long resourceSize=null;
Date resourceDate=null;
if (file.exists() && file.isFile()) {
jarSize = file.length();
jarDate = new Date(file.lastModified());
try {
JarFile jarFile = new JarFile(file, false);
ZipEntry entry = jarFile.getEntry(resourceFilename);
resourceSize = entry.getSize();
resourceDate = new Date(entry.getTime());
} catch (Throwable e) {
return ("Unable to open JAR" + jarFilename + " "+resourceUrl +"\n"+e.getMessage());
}
return "\nresource: "+resourceFilename+"\njar: "+jarFilename + " \nJarSize: " +jarSize+" \nJarDate: " +jarDate.toString()+" \nresourceSize: " +resourceSize+" \nresourceDate: " +resourceDate.toString()+"\n";
} else {
return("Unable to load jar:" + jarFilename+ " \nUrl: " +resourceUrl);
}
} catch (Exception e){
return e.getMessage();
}
}
With Linux, I'm using a small script to help me find in which jar a class lies that can be used in a find -exec:
findclass.sh:
unzip -l "$1" 2>/dev/null | grep $2 >/dev/null 2>&1 && echo "$1"
Basically, as jars are zip, unzip -l will print the list of class resources, so you'll have to convert . to /. You could perform the replacement in the script with a tr, but it's not too much trouble to do it yourself when calling the script.
The, the idea is to use find on the root of your classpath to locate all jars, then runs findclass.sh on all found jars to look for a match.
It doesn't handle multi-directories, but if you carefully choose the root you can get it to work.
Now, find which jar contains class org.apache.commons.lang3.RandomUtils to you un-mavenize your project (...):
$ find ~/.m2/repository/ -type f -name '*.jar' -exec findclass.sh {} org/apache/commons/lang3/RandomUtils \;
.m2/repository/org/apache/commons/commons-lang3/3.7/commons-lang3-3.7.jar
.m2/repository/org/apache/commons/commons-lang3/3.6/commons-lang3-3.6.jar
.m2/repository/org/apache/commons/commons-lang3/3.6/commons-lang3-3.6-sources.jar
$
I'm trying to access an XML file within a jar file, from a separate jar that's running as a desktop application. I can get the URL to the file I need, but when I pass that to a FileReader (as a String) I get a FileNotFoundException saying "The file name, directory name, or volume label syntax is incorrect."
As a point of reference, I have no trouble reading image resources from the same jar, passing the URL to an ImageIcon constructor. This seems to indicate that the method I'm using to get the URL is correct.
URL url = getClass().getResource("/xxx/xxx/xxx/services.xml");
ServicesLoader jsl = new ServicesLoader( url.toString() );
Inside the ServicesLoader class I have
XMLReader xr = XMLReaderFactory.createXMLReader();
xr.setContentHandler( this );
xr.setErrorHandler( this );
xr.parse( new InputSource( new FileReader( filename )));
What's wrong with using this technique to read the XML file?
Looks like you want to use java.lang.Class.getResourceAsStream(String), see
https://docs.oracle.com/javase/8/docs/api/java/lang/Class.html#getResourceAsStream-java.lang.String-
You don't say if this is a desktop or web app. I would use the getResourceAsStream() method from an appropriate ClassLoader if it's a desktop or the Context if it's a web app.
It looks as if you are using the URL.toString result as the argument to the FileReader constructor. URL.toString is a bit broken, and instead you should generally use url.toURI().toString(). In any case, the string is not a file path.
Instead, you should either:
Pass the URL to ServicesLoader and let it call openStream or similar.
Use Class.getResourceAsStream and just pass the stream over, possibly inside an InputSource. (Remember to check for nulls as the API is a bit messy.)
The problem was that I was going a step too far in calling the parse method of XMLReader. The parse method accepts an InputSource, so there was no reason to even use a FileReader. Changing the last line of the code above to
xr.parse( new InputSource( filename ));
works just fine.
I'd like to point out that one issues is what if the same resources are in multiple jar files.
Let's say you want to read /org/node/foo.txt, but not from one file, but from each and every jar file.
I have run into this same issue several times before.
I was hoping in JDK 7 that someone would write a classpath filesystem, but alas not yet.
Spring has the Resource class which allows you to load classpath resources quite nicely.
I wrote a little prototype to solve this very problem of reading resources form multiple jar files. The prototype does not handle every edge case, but it does handle looking for resources in directories that are in the jar files.
I have used Stack Overflow for quite sometime. This is the second answer that I remember answering a question so forgive me if I go too long (it is my nature).
This is a prototype resource reader. The prototype is devoid of robust error checking.
I have two prototype jar files that I have setup.
<pre>
<dependency>
<groupId>invoke</groupId>
<artifactId>invoke</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>node</groupId>
<artifactId>node</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
The jar files each have a file under /org/node/ called resource.txt.
This is just a prototype of what a handler would look like with classpath://
I also have a resource.foo.txt in my local resources for this project.
It picks them all up and prints them out.
package com.foo;
import java.io.File;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URI;
import java.net.URL;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
/**
* Prototype resource reader.
* This prototype is devoid of error checking.
*
*
* I have two prototype jar files that I have setup.
* <pre>
* <dependency>
* <groupId>invoke</groupId>
* <artifactId>invoke</artifactId>
* <version>1.0-SNAPSHOT</version>
* </dependency>
*
* <dependency>
* <groupId>node</groupId>
* <artifactId>node</artifactId>
* <version>1.0-SNAPSHOT</version>
* </dependency>
* </pre>
* The jar files each have a file under /org/node/ called resource.txt.
* <br />
* This is just a prototype of what a handler would look like with classpath://
* I also have a resource.foo.txt in my local resources for this project.
* <br />
*/
public class ClasspathReader {
public static void main(String[] args) throws Exception {
/* This project includes two jar files that each have a resource located
in /org/node/ called resource.txt.
*/
/*
Name space is just a device I am using to see if a file in a dir
starts with a name space. Think of namespace like a file extension
but it is the start of the file not the end.
*/
String namespace = "resource";
//someResource is classpath.
String someResource = args.length > 0 ? args[0] :
//"classpath:///org/node/resource.txt"; It works with files
"classpath:///org/node/"; //It also works with directories
URI someResourceURI = URI.create(someResource);
System.out.println("URI of resource = " + someResourceURI);
someResource = someResourceURI.getPath();
System.out.println("PATH of resource =" + someResource);
boolean isDir = !someResource.endsWith(".txt");
/** Classpath resource can never really start with a starting slash.
* Logically they do, but in reality you have to strip it.
* This is a known behavior of classpath resources.
* It works with a slash unless the resource is in a jar file.
* Bottom line, by stripping it, it always works.
*/
if (someResource.startsWith("/")) {
someResource = someResource.substring(1);
}
/* Use the ClassLoader to lookup all resources that have this name.
Look for all resources that match the location we are looking for. */
Enumeration resources = null;
/* Check the context classloader first. Always use this if available. */
try {
resources =
Thread.currentThread().getContextClassLoader().getResources(someResource);
} catch (Exception ex) {
ex.printStackTrace();
}
if (resources == null || !resources.hasMoreElements()) {
resources = ClasspathReader.class.getClassLoader().getResources(someResource);
}
//Now iterate over the URLs of the resources from the classpath
while (resources.hasMoreElements()) {
URL resource = resources.nextElement();
/* if the resource is a file, it just means that we can use normal mechanism
to scan the directory.
*/
if (resource.getProtocol().equals("file")) {
//if it is a file then we can handle it the normal way.
handleFile(resource, namespace);
continue;
}
System.out.println("Resource " + resource);
/*
Split up the string that looks like this:
jar:file:/Users/rick/.m2/repository/invoke/invoke/1.0-SNAPSHOT/invoke-1.0-SNAPSHOT.jar!/org/node/
into
this /Users/rick/.m2/repository/invoke/invoke/1.0-SNAPSHOT/invoke-1.0-SNAPSHOT.jar
and this
/org/node/
*/
String[] split = resource.toString().split(":");
String[] split2 = split[2].split("!");
String zipFileName = split2[0];
String sresource = split2[1];
System.out.printf("After split zip file name = %s," +
" \nresource in zip %s \n", zipFileName, sresource);
/* Open up the zip file. */
ZipFile zipFile = new ZipFile(zipFileName);
/* Iterate through the entries. */
Enumeration entries = zipFile.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
/* If it is a directory, then skip it. */
if (entry.isDirectory()) {
continue;
}
String entryName = entry.getName();
System.out.printf("zip entry name %s \n", entryName);
/* If it does not start with our someResource String
then it is not our resource so continue.
*/
if (!entryName.startsWith(someResource)) {
continue;
}
/* the fileName part from the entry name.
* where /foo/bar/foo/bee/bar.txt, bar.txt is the file
*/
String fileName = entryName.substring(entryName.lastIndexOf("/") + 1);
System.out.printf("fileName %s \n", fileName);
/* See if the file starts with our namespace and ends with our extension.
*/
if (fileName.startsWith(namespace) && fileName.endsWith(".txt")) {
/* If you found the file, print out
the contents fo the file to System.out.*/
try (Reader reader = new InputStreamReader(zipFile.getInputStream(entry))) {
StringBuilder builder = new StringBuilder();
int ch = 0;
while ((ch = reader.read()) != -1) {
builder.append((char) ch);
}
System.out.printf("zip fileName = %s\n\n####\n contents of file %s\n###\n", entryName, builder);
} catch (Exception ex) {
ex.printStackTrace();
}
}
//use the entry to see if it's the file '1.txt'
//Read from the byte using file.getInputStream(entry)
}
}
}
/**
* The file was on the file system not a zip file,
* this is here for completeness for this example.
* otherwise.
*
* #param resource
* #param namespace
* #throws Exception
*/
private static void handleFile(URL resource, String namespace) throws Exception {
System.out.println("Handle this resource as a file " + resource);
URI uri = resource.toURI();
File file = new File(uri.getPath());
if (file.isDirectory()) {
for (File childFile : file.listFiles()) {
if (childFile.isDirectory()) {
continue;
}
String fileName = childFile.getName();
if (fileName.startsWith(namespace) && fileName.endsWith("txt")) {
try (FileReader reader = new FileReader(childFile)) {
StringBuilder builder = new StringBuilder();
int ch = 0;
while ((ch = reader.read()) != -1) {
builder.append((char) ch);
}
System.out.printf("fileName = %s\n\n####\n contents of file %s\n###\n", childFile, builder);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
} else {
String fileName = file.getName();
if (fileName.startsWith(namespace) && fileName.endsWith("txt")) {
try (FileReader reader = new FileReader(file)) {
StringBuilder builder = new StringBuilder();
int ch = 0;
while ((ch = reader.read()) != -1) {
builder.append((char) ch);
}
System.out.printf("fileName = %s\n\n####\n contents of file %s\n###\n", fileName, builder);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
}
}
You can see a fuller example here with the sample output.
Here's a sample code on how to read a file properly inside a jar file (in this case, the current executing jar file)
Just change executable with the path of your jar file if it is not the current running one.
Then change the filePath to the path of the file you want to use inside the jar file. I.E. if your file is in
someJar.jar\img\test.gif
. Set the filePath to "img\test.gif"
File executable = new File(BrowserViewControl.class.getProtectionDomain().getCodeSource().getLocation().toURI());
JarFile jar = new JarFile(executable);
InputStream fileInputStreamReader = jar.getInputStream(jar.getJarEntry(filePath));
byte[] bytes = new byte[fileInputStreamReader.available()];
int sizeOrig = fileInputStreamReader.available();
int size = fileInputStreamReader.available();
int offset = 0;
while (size != 0){
fileInputStreamReader.read(bytes, offset, size);
offset = sizeOrig - fileInputStreamReader.available();
size = fileInputStreamReader.available();
}
Outside of your technique, why not use the standard Java JarFile class to get the references you want? From there most of your problems should go away.
If you use resources extensively, you might consider using
Commons VFS.
Also supports:
* Local Files
* FTP, SFTP
* HTTP and HTTPS
* Temporary Files "normal FS backed)
* Zip, Jar and Tar (uncompressed, tgz or tbz2)
* gzip and bzip2
* resources
* ram - "ramdrive"
* mime
There's also JBoss VFS - but it's not much documented.
I have 2 CSV files that I use to read data. The java program is exported as a runnable jar file. When you export it, you will figure out it doesn't export your resources with it.
I added a folder under project called data in eclipse. In that folder i stored my csv files.
When I need to reference those files I do it like this...
private static final String ZIP_FILE_LOCATION_PRIMARY = "free-zipcode-database-Primary.csv";
private static final String ZIP_FILE_LOCATION = "free-zipcode-database.csv";
private static String getFileLocation(){
String loc = new File("").getAbsolutePath() + File.separatorChar +
"data" + File.separatorChar;
if (usePrimaryZipCodesOnly()){
loc = loc.concat(ZIP_FILE_LOCATION_PRIMARY);
} else {
loc = loc.concat(ZIP_FILE_LOCATION);
}
return loc;
}
Then when you put the jar in a location so it can be ran via commandline, make sure that you add the data folder with the resources into the same location as the jar file.