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…
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
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 [icode]MyClass.class.getResourceAsStream(“myfile.png”)[/icode] 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 [icode]MyClass.class.getResourceAsStream("/res/myfile.png")[/icode] 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 [icode]MyClass.class.getClassLoader().getResourceAsStream(“res/myfile.png”)[/icode].
The best part to this long explanation: the linked StackOverflow post and ThinkPlexx article explained it nicely too.