Results 1 to 4 of 4
  1. #1
    Cyrus is offline Member
    Join Date
    Mar 2014
    Posts
    18
    Rep Power
    0

    Default getClass cannot be resolved

    I am trying to read a file that will be contained within the Jar. I have created a source folder in eclipse and put the file in there. The examples I can find online to do this are not working out for me. I am getting an error that getClass cannot be resolved.

    Java Code:
    private static void help() {
    	version();
    	try {
    		InputStreamReader is = getClass.getClassLoader().getResourceAsStream("data/cmdhelp");
    		int c;
    		do {
    			c = is.read();
    			System.out.print((char) c);
    		} while (c > -1);
    		System.out.println();
    	} catch (IOException e) {
    		System.out.println("JAR file has not been packaged correctly.");
    	}
    }
    The error from eclipse:
    getClass cannot be resolved Command.java /accface/src/frontend line 29 Java Problem

  2. #2
    Cyrus is offline Member
    Join Date
    Mar 2014
    Posts
    18
    Rep Power
    0

    Default Re: getClass cannot be resolved

    I found something else, and at least it finds getClass, but it still doesn't work. I wonder if this is a Java 1.7 feature, I'm using Java 1.6 at the moment because that is what Debian Linux has.

    Java Code:
    	private static void help() {
    		version();
    		try {
    			BufferedReader reader = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("data/cmdhelp")));
    			
    			int c;
    			do {
    				c = reader.read();
    				System.out.print((char) c);
    			} while (c > -1);
    			System.out.println();
    		} catch (IOException e) {
    			System.out.println("JAR file has not been packaged correctly.");
    		}
    	}
    Eclipse gives this error: Description Resource Path Location Type
    Cannot make a static reference to the non-static method getClass() from the type Object Command.java /accface/src/frontend line 30 Java Problem

  3. #3
    Cyrus is offline Member
    Join Date
    Mar 2014
    Posts
    18
    Rep Power
    0

    Default Re: getClass cannot be resolved

    Nevermind, I just switched on my brain and noticed the problem is that the method was static.

  4. #4
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: getClass cannot be resolved

    In your first code, you just had "getClass" without (). So it looked for such a variable which didn't exist.
    And yes, getClass() must be called on an object so in a non static method a getClass() is simply a this.getClass.

    If you just want the class in a static environment, then simply use Classname.class instead. (Where classname is replaced with the name of your class of course.)
    A static method cannot be overridden so it is ok to use it that way. If you want to get the class of an instance, you cannot use typeOfVariable.class because the type of the variable could be an interface or a superclass of the real type of the instance.

    Konrad

Similar Threads

  1. Terminating without error whenever getClass is used
    By Daryn in forum Advanced Java
    Replies: 1
    Last Post: 01-19-2014, 07:22 PM
  2. Replies: 3
    Last Post: 07-07-2012, 06:27 AM
  3. getClass().getResource()
    By forwardbias in forum AWT / Swing
    Replies: 11
    Last Post: 03-29-2012, 04:10 AM
  4. Java getClass()
    By hqt in forum New To Java
    Replies: 2
    Last Post: 01-31-2012, 09:44 PM
  5. using getField method of getClass
    By jdigger in forum New To Java
    Replies: 2
    Last Post: 02-03-2011, 12:16 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •