Setting up a self-contained LWJGL project

Started by CodeBunny, November 03, 2010, 15:24:51

Previous topic - Next topic

CodeBunny


jediTofu

 ::)  Oh, thought you were talking about going to src/lib, since that's what your code uses.  Yeah, it's impossible inside a jar, unless you use Web Start or an Applet (since they will extract the native libs for you).
cool story, bro

CodeBunny

Okay, I know this topic has been quiet for a bit, I just had one question:

I've gotten the extraction process working for the most part. If the programs are in a normal file hierarchy, everything runs fine - it will create a directory in the user.dir, and it will copy all the required libraries (this varies from OS to OS) into the file. After that, LWJGL can read the libraries and everything runs.

However, I'm running into a problem when the project is in a jar. If I create the data folder and fill it with the required libraries, everything runs on startup, but if I try to have the jar self-extract, it a) creates the directory, and b) begins writing the first file to copy, but 0 bytes are passed and nothing happens (even though it works fine outside of a jar).

I'm assuming I'm copying wrong. This is the code I use, is there anything wrong with it?

private static void copyFile(String directory, String filename, String targetDirectory)
	{
		InputStream in = LibraryLoader.class.getClassLoader().getResourceAsStream(directory + s + filename);
		OutputStream out = null;
		try
		{
			out = new FileOutputStream(targetDirectory + s + filename);
		}
		catch(FileNotFoundException e)
		{
			e.printStackTrace();
		}
		
		byte[] buf = new byte[1024];
        int len;
        try
        {
			while ((len = in.read(buf)) > 0)
			{
			    out.write(buf, 0, len);
			}
	        in.close();
	        out.close();
		}
        catch (IOException e)
        {
			e.printStackTrace();
		}
	}

Rene

Are you using backslashes as path separators? These only work for windows files. You should use forward slashes, these will work for every OS and for files inside ZIP files.
When I am king, they shall not have bread and shelter only, but also teachings out of books, for a full belly is little worth where the mind is starved - Mark Twain

broumbroum

Sometimes, the Inputstream will read 0 byte, before to continue (suppose you're downloading from a remote connection..).
while ((len = in.read(buf)) != -1) // this is -1 that indicate an EOF
			{
			    out.write(buf, 0, len);
			}

CodeBunny

Quote from: Rene on November 12, 2010, 01:01:18
Are you using backslashes as path separators? These only work for windows files. You should use forward slashes, these will work for every OS and for files inside ZIP files.

I actually detect the character that the OS natively uses for path separation and use that.

Quote from: broumbroum
Sometimes, the Inputstream will read 0 byte, before to continue (suppose you're downloading from a remote connection..).
while ((len = in.read(buf)) != -1) // this is -1 that indicate an EOF
			{
			    out.write(buf, 0, len);
			}


Hmm...

I tried changing it and got the same results. Thanks for the tip, though.

jediTofu

As already stated, you should be using "/" instead of the system-dependent one (i.e., "s" variable) for getting the resource.  The Java method expects a forward slash to obtain the resource, as that's the standard for Java.

http://download.oracle.com/javase/6/docs/api/java/lang/ClassLoader.html#getResource(java.lang.String)

However, getResourceAsStream(...) apparently didn't return null for you, which is what it would do if it couldn't find the resource.  I still think that the problem is in obtaining the resource though (perhaps wrong path, etc.).  To make sure that it's not the writing, try doing System.out.println(...); that way we know it's from the reading.  If it's from writing, try out.flush() right after out.write(...).
cool story, bro

CodeBunny

Can I just use "/" for Files and FileOutputStreams?

jediTofu

Quote from: CodeBunny on November 12, 2010, 14:21:15
Can I just use "/" for Files and FileOutputStreams?

Yes, but it won't hurt to use File.separator.
cool story, bro