I have an assignment in school to use a given .class file in java application. The file is for reading input that the user gives. I have created a .jar file containing the .class file and added it to my project library in NetBeans, but my application does not recognise it. How do I get to use the methods provided in the file/library?
Asked
Active
Viewed 464 times
0
-
You're going to been to be more specific. What is the `.class` file; what package is the `class` in. How did you package it into a `jar`? – Boris the Spider Mar 30 '14 at 13:30
-
I don't know what package the .class is, but I packaged it in the command line. – user3403621 Mar 30 '14 at 13:34
-
Right. Well, the package the class is in needs to be the same as the the folder that the class is in the Jar. That's why I ask exactly how you packaged it. [This](http://stackoverflow.com/q/7408921/2071828) might help you, or maybe your teacher gave you some information. – Boris the Spider Mar 30 '14 at 13:39
-
You could try adding the folder containing the class to your project, using the `Add JAR/Folder` option. – Boris the Spider Mar 30 '14 at 13:43
-
I can see the Lue.class file on the left, but when I try to implement this code int age=Lue.kluku(); i get error "Cannot find symbol symbol :variable Lue location: class JavaApplication4" – user3403621 Mar 30 '14 at 14:00
-
Do you know about imports? Have you correctly imported the class? – Boris the Spider Mar 30 '14 at 14:01
-
Yes I know, but I don't know how to add this one, becayse the package in which the .class is is
– user3403621 Mar 30 '14 at 14:05 -
Well, then you have [a problem](http://stackoverflow.com/a/2193298/2071828). – Boris the Spider Mar 30 '14 at 14:08
-
So what would be the easiest solution? – user3403621 Mar 30 '14 at 14:13
-
Okay, I did these steps: 1. created a new lavalibrary in netbeans 2. put the class file into: ...JavaLibrary/src/Lue/Lue.class 3. choosed the option "clean and build", and I got .jar file with package Lue 4. added the .jar to my project library and tried to put `import Lue.*;` but the program says that "package Lue does not exist". – user3403621 Mar 30 '14 at 14:29
-
The package of the class is named in the class file. You cannot change it without changing the class itself. Not sure that you have a solution beyond reflection. – Boris the Spider Mar 30 '14 at 14:30
-
So I uploaded the class here: http://rapidshare.com/share/0FF4B9E0B0D1F845AF4D3F52E0894BD1 can you check how the file is? – user3403621 Mar 30 '14 at 14:39
-
Okay, your are exactly correct. If you place _your_ code in the default package too then you should be fine. I would point out to your teacher that placing classes in the default package us **very** bad practice. It's even worse to force students trying to learn Java to do the same. – Boris the Spider Mar 30 '14 at 14:46
-
Okay, I put my mainclass in the default package, but what do I have to do with the jar file? – user3403621 Mar 30 '14 at 15:11
-
You can just include the folder containing the `class` file in your classpath. – Boris the Spider Mar 30 '14 at 15:47