简体   繁体   English

如何编译使用BlueJ的.jar文件类的代码?

[英]How can I compile code that uses a .jar file class with BlueJ?

I am learning java with BlueJ, and recently I was given a .jar file called Imagen.jar . 我正在用BlueJ学习java,最近我得到了一个名为Imagen.jar的.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 . 无论如何,我应该创建一个程序,使用一个名为Imagen的类。 Apparently, such class is within the mentioned .jar file. 显然,这样的类在提到的.jar文件中。

Clearly, BlueJ won't compile if I'm using such class since I have not imported it or anything. 很明显,如果我使用这样的类,BlueJ将无法编译,因为我没有导入它或任何东西。 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. 哪个不使用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? 如何导入.jar文件中包含的类?

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. 导航到包含jar文件的文件夹。 Select jar file. 选择jar文件。 Restart BlueJ. 重启BlueJ。

Answer extracted from this place 从这个地方提取的答案

你需要导入Imagen类,因为它在main方法中使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM