this.getClass (). getResource („”). getPath () zwraca niepoprawną ścieżkę

Obecnie wykonuję mały prosty program Java dla mojego Computer Science Final, który musi uzyskać ścieżkę bieżącej klasy uruchomionej. Pliki klas znajdują się w folderze gry C: 2013.

Aby uzyskać tę ścieżkę, nazywam ten segment kodu w moim głównym konstruktorze klasy:

public game(){
    String testPath = this.getClass().getResource("").getPath();
    //Rest of game
}

Jednak zamiast tego polecenie zwraca ten ciąg: „/” pomimo poprawnego wyjścia „C: / 2013 / game”. Dodatkowo próbowałem to naprawić, używając tego kodu:

public game(){
    String testPath = this.getClass().getClassLoader().getResource("").getPath();
}

Zwraca NullPointerException, który pochodzi z faktu, że getClassLoader () zwraca wartość null, pomimo pracy na moim środowisku Eclipse IDE. Jakieś pomysły?

questionAnswers(7)

yourAnswerToTheQuestion