Jump to content

[Solved][1.14.4] Get all files in specific folder


Recommended Posts

Posted (edited)

Currently, I have everything working in a dev environment where all java files are discovered in a certain folder and assigned to variables. My issue is locating the files in a compiled jar. Obviously, the path needs to change since the files are no longer java sources. I am unaware what exactly needs to be changed to what. I have searched for anything directory and path related, but have been unable to locate a solution so far. What steps do I need to take to find class files without importing? The entire purpose is to be modular so that any class (within the jar) in "com/zombe/zombecore/mods" can be removed or added.

 

modNums = Paths.get("src/main/java/com/zombe/zombecore/mods").toFile().exists() ? Paths.get("src/main/java/com/zombe/zombecore/mods").toFile().list().length : 0;//count the number of files in "com/zombe/zombecore/mods"
modFiles = Paths.get("src/main/java/com/zombe/zombecore/mods").toFile().listFiles();//list of files in the directory
try {
	for(int i =0; i < modNums; i++) {
		mod[i] = Class.forName("com.zombe.zombecore.mods."+modFiles[i].toString().substring(39, modFiles[i].toString().length()).replaceAll(".java", "").replaceAll(".class", ""));//assign each result to a class variable
	}
			
} catch (ClassNotFoundException e) {
	System.out.println(e);
}

 

Edited by Croa
Posted

If you want to be modular then do it with jars instead of classes

This is my Forum Signature, I am currently attempting to transform it into a small guide for fixing easier issues using spoiler blocks to keep things tidy.

 

As the most common issue I feel I should put this outside the main bulk:

The only official source for Forge is https://files.minecraftforge.net, and the only site I trust for getting mods is CurseForge.

If you use any site other than these, please take a look at the StopModReposts project and install their browser extension, I would also advise running a virus scan.

 

For players asking for assistance with Forge please expand the spoiler below and read the appropriate section(s) in its/their entirety.

  Reveal hidden contents

 

Posted

If I am ultimately reading from another jar file, I still need a path to identify and assign each file to a variable. Do you have an example how this will work or even terms to begin a search? 

Posted

Howdy

 

This might be promising?

https://stackoverflow.com/questions/17421590/iterate-over-folders-in-jar-file-directly

 

Java also has a stack of Reflection methods that would probably be suitable to iterate through a jar.

 

You might also get some clues from the Forge mod loading code, which (from a very quick look) appears to put a wrapper around a jar so that it appears to be a filesystem

see AbstractJarFileLocator

 

-TGG

Posted

You do know what your mod can use the code of pretty much any properly labelled jar in the mods folder, right?

This is my Forum Signature, I am currently attempting to transform it into a small guide for fixing easier issues using spoiler blocks to keep things tidy.

 

As the most common issue I feel I should put this outside the main bulk:

The only official source for Forge is https://files.minecraftforge.net, and the only site I trust for getting mods is CurseForge.

If you use any site other than these, please take a look at the StopModReposts project and install their browser extension, I would also advise running a virus scan.

 

For players asking for assistance with Forge please expand the spoiler below and read the appropriate section(s) in its/their entirety.

  Reveal hidden contents

 

Posted

Thanks for your assistance Umbra and Ghost. Here is ultimately what I got to work.

try {
	URI uri = ZombeCore.class.getClassLoader().getResource("com/zombe/zombecore/mods").toURI();
	Path myPath;
	if (uri.getScheme().equals("modjar")) {
		System.out.println(" | Jar loaded | ");

		JarFile jarFile = new JarFile(Minecraft.getInstance().gameDir+"/mods/"+NAME+"-"+VERSION+".jar");
		final Enumeration<JarEntry> entries = jarFile.entries();

		while(entries.hasMoreElements()) {
			JarEntry entry = (JarEntry)entries.nextElement();
			String name = entry.getName();
			if(name.startsWith("com/zombe/zombecore/mods/Z") && name.endsWith(".class"))
				arrMods.add(Class.forName(name.replaceAll("/", ".").replaceAll(".class", "")));
		}
	} else {
		System.out.println(" | Source loaded | ");
		myPath = Paths.get(uri);
		Stream<Path> walk = Files.walk(myPath, 1);
		int temp = 0;
		for (Iterator<Path> it = walk.iterator(); it.hasNext();){
			if(temp ==0) {
				it.next(); temp = 1;
			}
			arrMods.add(Class.forName("com.zombe.zombecore.mods."+it.next().getFileName().toString().replaceAll(".class", "")));
		}walk.close();
	}

}catch (URISyntaxException e) {}catch (IOException e) {}catch (ClassNotFoundException e) {}

 

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.