Intellij, how to use a .class(without source code) in my own project

Nikolay Pankov :

Due to a suggestion I have decided to add the whole project to github, so it's easier for experts to help me :/. I've been googling around for hours and can't seem to fix it. https://github.com/NicholasExxonite/test_lab_week13

Alright. So this is from a lab class, we where given an archive with 2 .java files, one is Ball and another Triangle(they contain the class and methods to create the shapes) and 5 .class files called GameArena(the purpose of being it only a .class file and not the whole source code is so that we cannot change it). I need to create a main class and create instances of these classes and call their methods to create a working game. The triangle and ball .java files are okay, I can access them and call them. However my intellij just doesn't want to recognise GameArena.class. The contents of the given archive : https://imgur.com/JJMtO6J

I am a new java/intelliJ user running the 2018.2.7(build 182.5107.41)version with SDK 11.0.2. I decompressed the archive in my project's source folder, from then Ball and Triangle were usable, but GameArena not. So I tried adding it's path from Files-Project Structure-Modules, then I tried to add it to classpath in the platform setting. None of them work, when I try to build and run the main method I get the following error:

Error:(4, 9) java: cannot find symbol
  symbol:   class GameArena
  location: class testGame
Error:(4, 33) java: cannot find symbol
  symbol:   class GameArena
  location: class testGame

Screenshot: https://imgur.com/TSryZMy

public class testGame {
    public static void main (String Args[]){
        Ball newBall = new Ball(100, 100, 50, "Yellow");
        GameArena newGame = new GameArena(200, 200);
    }
}
hce :

You should do the following:

1. Copy your Gamearena Class files to a new Directory

  • Right Click on your test_lab_week13
  • new Directory 'Libs'
  • Copy all your .class files for Gamearena to Libs (simply per drag&drop)

enter image description here

2. Add your Libs directory as dependency to your module

  • Files > Project Structure... (Ctrl + Alt + + Shift + S)
  • Set Project SDK to Oracle JDK 1.8 (11 will not work, because it doesnt support JavaFX)

enter image description here

  • Add Lib Directory to your module dependency, by selecting Dependencies tab and add with +

enter image description here

3. Build your Project and start it

  • Build > Build Project (Ctrl + F9)
  • Run > Run testgame

Let me know if it helps.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=160860&siteId=1