>>> URI is not hierarchical - java

Sorry I'm new here but I have and issue I'm hoping someone can help me solve.
This code runs perfect while in eclipse, but after compiled it say's:
java.lang.IllegalArgumentException: URI is not hierarchical
Any help would be appropriated, thanks!
public void loadMods(String pkg) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException {
List<Class<?>> classes = getClasses(pkg);
for(Class<?> c : classes) {
for (Method m : c.getMethods()) {
Object o = null;
o = c.newInstance();
if (m.getName().contains("load")) {
m.setAccessible(true);
m.invoke(o);
}
}
}
}
public static List<Class<?>> getClasses(String pkg) {
String pkgname = pkg;
List<Class<?>> classes = new ArrayList<Class<?>>();
File directory = null;
String fullPath;
String relPath = pkgname.replace('.', '/');
URL resource = ClassLoader.getSystemClassLoader().getResource(relPath);
if (resource == null) {
throw new RuntimeException("No resource for " + relPath);
}
fullPath = resource.getFile();
try {
directory = new File(resource.toURI());
} catch (URISyntaxException e) {
throw new RuntimeException(pkgname + " (" + resource + ") invalid URL / URI.", e);
} catch (IllegalArgumentException e) {
directory = null;
}
if (directory != null && directory.exists()) {
String[] files = directory.list();
for (int i = 0; i < files.length; i++) {
if (files[i].endsWith(".class")) {
String className = pkgname + '.' + files[i].substring(0, files[i].length() - 6);
try {
classes.add(Class.forName(className));
} catch (ClassNotFoundException e) {
throw new RuntimeException("ClassNotFoundException loading " + className);
}
} else {
String pkgnamex = pkgname + '.' + files[i];
List<Class<?>> classesx = new ArrayList<Class<?>>();
File directoryx = null;
String fullPathx;
String relPathx = pkgnamex.replace('.', '/');
URL resourcex = ClassLoader.getSystemClassLoader().getResource(relPathx);
if (resourcex == null) {
throw new RuntimeException("No resource for " + relPathx);
}
fullPathx = resourcex.getFile();
try {
directoryx = new File(resourcex.toURI());
} catch (URISyntaxException e) {
throw new RuntimeException(pkgnamex + " (" + resourcex + ") invalid URL / URI.", e);
} catch (IllegalArgumentException e) {
directoryx = null;
}
if (directoryx != null && directoryx.exists()) {
String[] filesx = directoryx.list();
for (int ix = 0; ix < filesx.length; ix++) {
if (filesx[ix].endsWith(".class")) {
String classNamex = pkgnamex + '.' + filesx[ix].substring(0, filesx[ix].length() - 6);
try {
classes.add(Class.forName(classNamex));
} catch (ClassNotFoundException e) {
throw new RuntimeException("ClassNotFoundException loading " + classNamex);
}
}
}
}
}
}
}
return classes;
}

When you run the code from within Eclipse it uses the compiled classes (by default in folder 'target'). However if you run the code from external normally you use a JAR file created by Eclipse.
And this problem arises when referencing something inside the JAR which is explained by the linked questions.
In short: URIs in the file system are syntactically correct. An URI referencing something into a JAR is no more a valid URI.

Related

How can I fix the following problem regarding the variable?

I have a class and inside it there is a baseDir variable which has been defined as follows:
public class experiment {
for (int exp = 0; exp < experimentCnt; exp++) {
String dirString = config.getClass().getSimpleName() + "_" + df.format(new Date());
String baseDir = new File(homeDir + "/" + dirString).getAbsolutePath();
System.out.println("Running simulation: " + dirString);
setCurrentDirectory(baseDir);
PrintWriter paramsLog = null;
try {
paramsLog = new PrintWriter(
new File("experimentParams.log").getAbsoluteFile(), "UTF-8");
paramsLog.println(params);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
Now, I want to use that baseDir variable in another class. How can I make it accessible?
Instead of making a new variable inside your function, you should just make a public variable outside of your function.
public class experiment {
public String baseDir;
for (int exp = 0; exp < experimentCnt; exp++) {
String dirString = config.getClass().getSimpleName() + "_" + df.format(new Date());
baseDir = new File(homeDir + "/" + dirString).getAbsolutePath();
System.out.println("Running simulation: " + dirString);
setCurrentDirectory(baseDir);
PrintWriter paramsLog = null;
try {
paramsLog = new PrintWriter(
new File("experimentParams.log").getAbsoluteFile(), "UTF-8");
paramsLog.println(params);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}

Java hash checking

For some reason i can't figure out why it downloads the new applet every time even though i have the newest application on my computer already (that's what this downloader/checker does, to check the hash, and if its outdated, it re-downloads the newer version which is uploaded to web host)
My downloader class
public class Downloader {
private static final String HASH_URL = "/current";
private static final String DOWNLOAD_URL = ".jar";
private LoadingFrame loadingFrame;
public Downloader(LoadingFrame loadingFrame) {
this.loadingFrame = loadingFrame;
}
private String getLatestHash() {
loadingFrame.setLoadingText("Checking if client is up to date...");
try (InputStream in = new URL(HASH_URL).openStream()) {
return new String(IOUtils.toByteArray(in)).trim();
} catch (IOException e) {
loadingFrame.setLoadingText("Error loading client [ErrorCode: 7A]");
throw new RuntimeException("Error loading client");
}
}
public File downloadLatestPack() {
try {
File dir = new File(System.getProperty("user.home") + File.separator + "Project" + File.separator + "client");
if (!dir.exists()) {
dir.mkdirs();
}
loadingFrame.setLoadingText("Checking if client is up to date...");
String latestHash = getLatestHash();
File latest = new File(dir.getPath() + File.separator + latestHash + ".jar");
if (!latest.exists() || !com.google.common.io.Files.hash(latest, Hashing.sha1()).toString().equals(latestHash)) {
loadingFrame.setLoadingText("Doing some house keeping...");
for (File f : dir.listFiles()) {
if (f.getName().endsWith(".jar") && !f.getName().equals(latest.getName())) {
f.delete();
}
}
loadingFrame.setLoadingText("Downloading latest client...");
latest.createNewFile();
try (InputStream in = new URL(DOWNLOAD_URL).openStream()) {
Files.copy(in, latest.toPath(), StandardCopyOption.REPLACE_EXISTING);
}
} else {
loadingFrame.setLoadingText("Client is up to date!");
}
return latest;
} catch (IOException e) {
loadingFrame.setLoadingText("Error loading client [ErrorCode: 6B]");
throw new RuntimeException("Error loading client");
}
}
}

File.delete() fails to delete files in a directory

After writing the text files into a directory, i am trying to delete the empty files written by the PrintWriter.
File.delete() function fails to delete the file. Below is the code for writing and deleting.
private static void writeFile(ArrayList<ArrayList<String>> listRowVal, String szOutputDir, ArrayList<String> listHeader){
PrintWriter pw = null;
try {
ArrayList<String> listCells = listRowVal.get(0);
int iCells = listCells.size();
for(int k=0; k<iCells; k++){
String language = listHeader.get(k);
String szFileName = "files_"+ language +".csv";
pw = new PrintWriter(new FileWriter(szOutputDir + File.separator + szFileName));
for(ArrayList<String> listNCRCellVal : listRowVal){
String szVal = listNCRCellVal.get(k);
if(szVal != null && szVal.trim().length() > 0){
pw.println(szVal);
}
pw.flush();
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
if(pw != null){
pw.close();
pw = null;
}
//System.gc();
deleteEmptyFiles(szOutputDir);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private static void deleteEmptyFiles(String szDirPath) {
File file = new File(szDirPath);
if (file.isDirectory()) {
String[] files = file.list();
if (files.length > 0) {
for (String szFileName : files) {
File deleteFile = new File(szDirPath + File.separator + szFileName);
if (deleteFile.length() == 0) {
//deleteFile.setWritable(true, false);
boolean bdeleted = deleteFile.delete();
if(bdeleted){
System.out.println(deleteFile.getName() + " deleted.");
}
}
}
}
}
}
What is going wrong..??
You must close each PrintWriter, i.e. pw.close() must be on the end of "k" loop.

Iterate over folders in JAR file directly

I have the following code to iterate over folders and files in the class path and determine the classes and get a field with a ID and print them out to a logger. This is working fine if I run this code in my IDE, but if I package my project into a JAR file and this JAR file into a EXE file with launch4j, I can't iterate over my classes again.
I get the following path if I try to iterate over my classes in the JAR/EXE file:
file:/C:/ENTWICKLUNG/java/workspaces/MyProject/MyProjectTest/MyProjectSNAPSHOT.exe!/com/abc/def
How can I achieve this to iterate over all my classes in my JAR/EXE file?
public class ClassInfoAction extends AbstractAction
{
/**
* Revision/ID of this class from SVN/CVS.
*/
public static String ID = "#(#) $Id ClassInfoAction.java 43506 2013-06-27 10:23:39Z $";
private ClassLoader classLoader = ClassLoader.getSystemClassLoader();
private ArrayList<String> classIds = new ArrayList<String>();
private ArrayList<String> classes = new ArrayList<String>();
private int countClasses = 0;
#Override
public void actionPerformed(ActionEvent e)
{
countClasses = 0;
classIds = new ArrayList<String>();
classes = new ArrayList<String>();
getAllIds();
Iterator<String> it = classIds.iterator();
while (it.hasNext())
{
countClasses++;
//here I print out the ID
}
}
private void getAllIds()
{
String tempName;
String tempAbsolutePath;
try
{
ArrayList<File> fileList = new ArrayList<File>();
Enumeration<URL> roots = ClassLoader.getSystemResources("com"); //it is a path like com/abc/def I won't do this path public
while (roots.hasMoreElements())
{
URL temp = roots.nextElement();
fileList.add(new File(temp.getPath()));
GlobalVariables.LOGGING_logger.info(temp.getPath());
}
for (int i = 0; i < fileList.size(); i++)
{
for (File file : fileList.get(i).listFiles())
{
LinkedList<File> newFileList = null;
if (file.isDirectory())
{
newFileList = (LinkedList<File>) FileUtils.listFiles(file, TrueFileFilter.INSTANCE, TrueFileFilter.INSTANCE);
if (newFileList != null)
{
for (int j = 0; j < newFileList.size(); j++)
{
tempName = newFileList.get(j).getName();
tempAbsolutePath = newFileList.get(j).getAbsolutePath();
checkIDAndAdd(tempName, tempAbsolutePath);
}
}
}
else
{
tempName = file.getName();
tempAbsolutePath = file.getAbsolutePath();
checkIDAndAdd(tempName, tempAbsolutePath);
}
}
}
getIdsClasses();
}
catch (IOException e)
{
}
}
private void checkIDAndAdd(String name, String absolutePath)
{
if (name.endsWith(".class") && !name.matches(".*\\d.*") && !name.contains("$"))
{
String temp = absolutePath.replace("\\", ".");
temp = temp.substring(temp.lastIndexOf(/* Class prefix */)); //here I put in the class prefix
classes.add(FilenameUtils.removeExtension(temp));
}
}
private void getIdsClasses()
{
for (int i = 0; i < classes.size(); i++)
{
String className = classes.get(i);
Class<?> clazz = null;
try
{
clazz = Class.forName(className);
Field idField = clazz.getDeclaredField("ID");
idField.setAccessible(true);
classIds.add((String) idField.get(null));
}
catch (ClassNotFoundException e1)
{
}
catch (NoSuchFieldException e)
{
}
catch (SecurityException e)
{
}
catch (IllegalArgumentException e)
{
}
catch (IllegalAccessException e)
{
}
}
}
}
You cannot create File objects from arbitrary URLs and use the usual filesystem traversal methods. Now, I'm not sure if launch4j does make any difference, but as for iterating over the contents of plain JAR file, you can use the official API:
JarURLConnection connection = (JarURLConnection) url.openConnection();
JarFile file = connection.getJarFile();
Enumeration<JarEntry> entries = file.entries();
while (entries.hasMoreElements()) {
JarEntry e = entries.nextElement();
if (e.getName().startsWith("com")) {
// ...
}
}
Above snippet lists all the entries in the JAR file referenced by url, i.e. files and directories.

Listing classes in a package's subdirectory

I'm not sure if I'm using the correct terminology here.. but if my package name is set up like this:
com.example.fungame
-ClassA
-ClassB
-com.example.fungame.sprite
-ClassC
-ClassD
How can I programmatically get an array (a Class[] I'm guessing) of all the classes in the .sprite subdirectory?
Try this method:
public static Class[] getClasses(String pckgname) throws ClassNotFoundException {
ArrayList classes=new ArrayList();
File directory = null;
try {
directory = new File(Thread.currentThread().getContextClassLoader().getResource(pckgname.replace('.', '/')).getFile());
} catch(NullPointerException x) {
throw new ClassNotFoundException(pckgname + " does not appear to be a valid package");
}
if (directory.exists()) {
// Get the list of the files contained in the package
String[] files = directory.list();
for (int i = 0; i < files.length; i++) {
// we are only interested in .class files
if(files[i].endsWith(".class")) {
// removes the .class extension
try {
Class cl = Class.forName(pckgname + '.' + files[i].substring(0, files[i].length() - 6));
classes.add(cl);
} catch (ClassNotFoundException ex) {
}
}
}
} else {
throw new ClassNotFoundException(pckgname + " does not appear to be a valid package");
}
Class[] classesA = new Class[classes.size()];
classes.toArray(classesA);
return classesA;
}

Categories

Resources