I am learning java with BlueJ, and recently I was given a .jar file called Imagen.jar. Apparently, what it does is return some pixel vectors depending on image file names given as parameters to it.
Anyway, I am supposed to make a program that will use a class called Imagen. Apparently, such class is within the mentioned .jar file.
Clearly, BlueJ won’t compile if I’m using such class since I have not imported it or anything. But, I don’t really know how to import such class in the first place.
I was given the following example code:
import java.io.PrintWriter;
public class Main {
public static void main(String arg[ ]){
if(arg.length > 1){
Imagen imagen = new Imagen(arg[0]);
int [][] m = imagen.getMatriz();
PrintWriter salida = null;
try {
salida = new PrintWriter(arg[1]);
}
catch(Exception e){
System.err.println(e);
}
for(int [] fila : m ){
for(int valor : fila){
System.out.print("\t"+valor);
salida.print("\t"+valor);
}
salida.println("");
System.out.println("");
}
if(salida!=null){
salida.close();
}
}
else {
System.out.println("Uso: java -classpath .;Imagen.jar Main nombreArchivo.gif");
}
}
}
Which does not compile using BlueJ. However, as you can see, at the end it says that to use it, you have to type in the terminal:
java -classpath .;Imagen.jar Main myImageFile.gif
And I do it. But it keeps throwing me the same message.
So I am stuck right now:
- Why is the terminal line I was told to use not working?
- How can I import the class that is contained within a .jar file?
You need to do the following once.
Select the menu option Tools -> Preferences.
In the resulting dialog, click on the Libraries tab.
Click the Add button.
Navigate to the folder containing jar file. Select jar file.
Restart BlueJ.
Answer extracted from this place