Search code examples
javanetbeans-8

Can't use class from jar in netbeans


libraries properties

For one of my java courses, I need to import a pre-compiled java class in netbeans. I can't use it apparently.

So my prof is using grasp, which I don't like as IDE so I'm using netbeans. She is useless for my problem, so the problem here is that she has compiled the Joueur.class from java libraries and she wanted that we use it in our project. So I managed to transform the class in a jar file with jar.exe -cf, then I added it with right-clicking on libraries in my project in netbeans. Netbeans listed i,so it apparently recognized it. But when I try to use methods from it, it's underlined. But I've read that netbeans will recognized the contents of the jar automatically so I should be able to use it without a problem. Is there an import to do in the main package or something to use it? I can't find the information ... global netbeans


Solution

  • The name of the package for the class has to match the directory path in the jar. So if you want to create the jar correctly for class x.y.MyClass, you need to create the directories for it, copy the class file to it and then use jar to create the jar file. Here are some example commands:

    mkdir x
    mkdir x\y
    copy MyClass.class x\y
    jar -cf MyJar.jar x
    

    To verify, type the following:

    jar -tf MyJar.jar
    

    It should print out something like:

    META-INF/
    META-INF/MANIFEST.MF
    x/y/
    x/y/MyClass.class