Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (109)
games submitted by our members
Games in WIP (536)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: 1 [2]
  ignore  |  Print  
  [LWJGL] Game not running when used in JarSplice  (Read 2203 times)
0 Members and 1 Guest are viewing this topic.
Offline Mr.CodeIt

Junior Member





« Reply #30 - Posted 2014-05-09 21:38:29 »


This is how I did it with Java2D. Right now the
1  
img  = this.getClass().getResource("foo.txt");
gives an error. I'll try to rephrase the question, How do I load textures from the Slick_util.jar into a fat jar. I watched videos on how and I can easily get the libraries and jars, it's just I don't have the textures when I run it. I put the res/ folder in the src/ folder now. The textures don't load still ofcourse.

I'm not drunk, I've just been up late coding.
Offline BurntPizza
« Reply #31 - Posted 2014-05-09 21:45:01 »

This is how I did it with Java2D. Right now the
1  
img  = this.getClass().getResource("foo.txt");
gives an error. I'll try to rephrase the question, How do I load textures from the Slick_util.jar into a fat jar. I watched videos on how and I can easily get the libraries and jars, it's just I don't have the textures when I run it. I put the res/ folder in the src/ folder now. The textures don't load still ofcourse.

Either use
1  
img  = this.getClass().getClassLoader().getResourceAsStream("res/foo.txt");
or
1  
img  = this.getClass().getResourceAsStream("/res/foo.txt");


Notice the "/" if you don't use getClassLoader(). Also, the AsStream part is necessary.
Offline Mr.CodeIt

Junior Member





« Reply #32 - Posted 2014-05-10 01:56:30 »

This is how I did it with Java2D. Right now the
1  
img  = this.getClass().getResource("foo.txt");
gives an error. I'll try to rephrase the question, How do I load textures from the Slick_util.jar into a fat jar. I watched videos on how and I can easily get the libraries and jars, it's just I don't have the textures when I run it. I put the res/ folder in the src/ folder now. The textures don't load still ofcourse.

Either use
1  
img  = this.getClass().getClassLoader().getResourceAsStream("res/foo.txt");
or
1  
img  = this.getClass().getResourceAsStream("/res/foo.txt");


Notice the "/" if you don't use getClassLoader(). Also, the AsStream part is necessary.

I forgot the code to create Images and load them. I deleted the code I used to remember, but can't I just do this with the textures?
1  
wooda = TextureLoader.getTexture("PNG", new FileInputStream(new File("res/Baddy.png")));

This works except for when I eport the jar in Jarsplice. That is what I learned but someone suggested this which ddn't work:
1  
wood = TextureLoader.getTexture("PNG", MainFS.class.getClassLoader().getResourceAsStream("res/FlubberFlap.png"));


error:
1  
2  
3  
4  
Exception in thread "main" java.lang.NullPointerException
   at org.newdawn.slick.opengl.TextureLoader.getTexture(TextureLoader.java:64)
   at org.newdawn.slick.opengl.TextureLoader.getTexture(TextureLoader.java:24)
   at FSMain.MainFS.main(MainFS.java:67)


Instead of slick_util, should I just use the normal image rendering somehow Huh I have no idea what other way to draw and load them .

I'm not drunk, I've just been up late coding.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline SHC
« Reply #33 - Posted 2014-05-10 04:06:57 »

Can I have your screenshot of the package tree? Expand it completely and send us a screenshot.

Offline Mr.CodeIt

Junior Member





« Reply #34 - Posted 2014-05-10 05:15:34 »

Can I have your screenshot of the package tree? Expand it completely and send us a screenshot.


This is the whole Project. FSMain is the package I'm working with. MainFS is the main class.:




This is how I loaded textures

based on https://www.youtube.com/watch?v=naE3nbreSUo  Pointing

I'll message you the full cource code for the main class  if you want. It's not too long, It's just the drawing methods that  take up space.

I'm not drunk, I've just been up late coding.
Offline AppleSauce
« Reply #35 - Posted 2014-05-10 06:19:44 »

I have somewhat of the same problem too. You should just continue your work and figure it out some other time.
Offline BurntPizza
« Reply #36 - Posted 2014-05-10 06:23:04 »

This is how I loaded textures

based on https://www.youtube.com/watch?v=naE3nbreSUo  Pointing

For the last time,
new File(...)
Will. Not. Work. Why? Because those "files" you want to load are zip entries when you export to jar (jar files are actually zip files), not files.
Offline Mr.CodeIt

Junior Member





« Reply #37 - Posted 2014-05-10 06:43:36 »

I remember that. Should I start off with
1  
File img = new File("/res/ImageThingy.png");
then
1  
wood = TextureLoader.getTexture("png", new FileInputStream(img));
The
1  
res/
is a source folder.

I'm not drunk, I've just been up late coding.
Offline Mr.CodeIt

Junior Member





« Reply #38 - Posted 2014-05-10 07:27:44 »

If I'm unclear still, this is exactly what I'm doing.At the very end my results are a blank window for about a second. Please tell me what Im doing wrong, I really need to understand:
Video of what I did: http://www.mediafire.com/watch/oohf1qnh2oefkxp/exportsfail.mp4

I'm not drunk, I've just been up late coding.
Offline SHC
« Reply #39 - Posted 2014-05-10 10:48:56 »

Never use Files when packing them inside the JAR file. You have to do this.

1  
InputStream stream = this.getClass().getClassLoader().getResourceAsStream("res/ImageThingy.png");

Then

1  
wood = TextureLoader.getTexture("png", stream);

The
res/
can be either source folder or a package, anything will work.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Mr.CodeIt

Junior Member





« Reply #40 - Posted 2014-05-10 18:07:43 »

Never use Files when packing them inside the JAR file. You have to do this.

1  
InputStream stream = this.getClass().getClassLoader().getResourceAsStream("res/ImageThingy.png");

Then

1  
wood = TextureLoader.getTexture("png", stream);

The
res/
can be either source folder or a package, anything will work.

Great, I understand this. Could you give an example of how to use it, I keep getting a static error.

I'm not drunk, I've just been up late coding.
Offline SHC
« Reply #41 - Posted 2014-05-10 18:15:50 »

What error are you getting? Post the stackstrace.

Offline Mr.CodeIt

Junior Member





« Reply #42 - Posted 2014-05-10 18:30:07 »

1  
2  
3  
4  
Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
   Cannot use this in a static context

   at FSMain.MainFS.main(MainFS.java:68)

I'm not drunk, I've just been up late coding.
Offline Gibbo3771
« Reply #43 - Posted 2014-05-10 18:55:15 »

1  
2  
3  
4  
Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
   Cannot use this in a static context

   at FSMain.MainFS.main(MainFS.java:68)


Here is an example of what the problem is...


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
public class Foo{

    public static SomeOtherClass someOtherClass =  new SomeOtherClass();

    public Foo(){
   
    // Will not compile, you can not use this to reference a static object
   aMethod(this.someOtherClass);


    // Instead you would do this
   aMethod(Foo.someOtherClass);

    }

    public void aMethod(SomeOtherClass someOtherClass){
           // do something
   }

}


I hope I have that right lol

"This code works flawlessly first time and exactly how I wanted it"
Said no programmer ever
Offline ra4king

JGO Kernel


Medals: 342
Projects: 2
Exp: 5 years


I'm the King!


« Reply #44 - Posted 2014-05-10 20:37:20 »

My god this thread is a whole mess.

I advise you all to read this carefully:

Mr.CodeIt, your problems with the command line earlier were a huge shock to me. If you don't know your way around a command line, then you should not be using an IDE like Eclipse. I cannot stress this enough and how important to learn to compile and run from the command line. You will not get anywhere in life without it.

Now for loading resources, I will explain like I've explained too many times in this forum:
java.io.File can only be used to load files on the file system. If the file path you provide to File is a relative path, meaning it does not start with a forward slash or drive letter, it will look relative to the Current Directory. The Current Directory is the directory the program is run from. In Eclipse, this is the root of the project. Since your "res" folder is in the root of the project, then the files are found and all is fine.

However when exporting, since your resources are inside a JAR file (and a JAR file is essentially a ZIP file), you cannot use File. Instead you must use Class.getResource or Class.getResourceAsStream. Both methods find the file the same way, the only exception is that the first returns a java.net.URL object and the second returns a java.io.InputStream, so you use whichever one you need.

Now both methods have 2 ways of looking for the files:
- If the provided String does not begin with a leading forward slash, then it looks relative to the location of the Class object you called them on. What is a Class object? Every Java class has an equivalent java.lang.Class instance that goes along with it and there are two ways to get it: MyClass.class and using getClass(). MyClass.class is special syntax that returns MyClass's Class object. getClass() is a method inside java.lang.Object (which all classes automatically inherit) and it returns the current instance's Class object. It is best to use the first way as the second returns the Class type that was created, which may be an inherited class! Now for example, say you have a class "my.awesome.package.MyClass", calling
MyClass.class.getResourceAsStream("myfile.png")
will look inside the "my/awesome/package/" folder for a file named "myfile.png".

- If the provided String does begin with a leading forward slash, then it looks relative to the folder containing the root package, which in Eclipse is the "src" folder. This is why everybody was telling you to move the "res" folder under the "src" folder, as doing
MyClass.class.getResourceAsStream("/res/myfile.png")
will find it at "src/res/myfile.png". A nice little trick also is to use getClassLoader() as you have seen. Using a leading forward slash with Class.getResourceAsStream (and getResource) actually calls ClassLoader.getResourceAsStream without the leading forward slash. Both have the same behavior. Therefore, "src/res/myfile.png" can also be found doing
MyClass.class.getClassLoader().getResourceAsStream("res/myfile.png")
.

The best part to this long explanation: the linked StackOverflow post and ThinkPlexx article explained it nicely too.

Pages: 1 [2]
  ignore  |  Print  
 
 

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

CogWheelz (18 views)
2014-07-30 21:08:39

Riven (25 views)
2014-07-29 18:09:19

Riven (15 views)
2014-07-29 18:08:52

Dwinin (12 views)
2014-07-29 10:59:34

E.R. Fleming (33 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

pw (43 views)
2014-07-24 01:59:36

Riven (43 views)
2014-07-23 21:16:32

Riven (30 views)
2014-07-23 21:07:15

Riven (31 views)
2014-07-23 20:56:16
List of Learning Resources
by SilverTiger
2014-07-31 18:29:50

List of Learning Resources
by SilverTiger
2014-07-31 18:26:06

List of Learning Resources
by SilverTiger
2014-07-31 13:54:12

HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!