如何在java程序中调用jar文件?

时间:2011-09-21 15:45:14

标签: java

大家好..    我有一个java程序将Html转换为文本文件...    但我想把一个stanford-postagger jar文件调用到我的java pgm ..    任何人都可以帮助我..

这是我的JAVA程序将HTML转换为TXT

      import java.io.BufferedReader;
         import java.io.FileOutputStream;
                   import java.io.FileWriter;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintStream;
import java.util.StringTokenizer;
import java.util.Scanner;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.File;

public class conv {


    public static void main(String[] args) throws IOException {




    StringBuilder sb = new StringBuilder();
     BufferedReader br = new BufferedReader(new FileReader("C:/1.html"));
     String line;
     while ( (line=br.readLine()) != null) 
     {
       sb.append(line);

    }
     try
     {
         String nohtml = sb.toString().replaceAll("\\<.*?>","");
         PrintStream out = new PrintStream(new FileOutputStream("C:/Out.txt"));
         StringTokenizer st = new StringTokenizer(nohtml);
         while (st.hasMoreTokens())
         {
             out.println(st.nextToken().toString());
         }

           Scanner scan = new Scanner(System.in);
    System.out.print("Enter File Name:");
            String file_Name = "C:/"+scan.nextLine();
    //System.out.print("Enter Starting Word:");
    String start_word = "Good";
    //System.out.print("Enter Final Word:");
    String end_word = "Views,";

            conv file = new conv();
    String word = file.showLines(file_Name, start_word,end_word);

    System.out.println(">>>");
    if(word.length()>1)
        {
              //System.out.println(word);
    String[] words = word.split("\\,");
            for (String str : words)
            {
              System.out.println(str);

            try
            {
            PrintStream out1 = new PrintStream(new 

FileOutputStream("c:/sampleoutput.txt"));
                for (String astr : words)
                {
                  out1.println(astr);
                }
                out1.println();
               out1.close();            
         }  
                  /*StringTokenizer stg = new StringTokenizer(word, ".");
                    File newFile = new File("c:/newWords.txt");
                  try {

                        BufferedWriter writer= new BufferedWriter(new FileWriter(newFile,true));

                        while(stg.hasMoreTokens()) {
                            String val = stg.nextToken().trim()+".";
                            System.out.println(val);
                            writer.append(val);
                            writer.newLine();
                        }
                        writer.close();
                    }*/
    catch (Exception e) 
    {
                       // e.printStackTrace();
System.out.println(e);
                    }

        }  
        }


        else
            System.out.println("### Not Found");


}

catch(Exception ex)
         {
             System.out.println(ex);
         }

}


public String showLines(String fileName, String start, String end)  {

    String word = "";
    try {
        Scanner scan = new Scanner(new File(fileName));
        while(scan.hasNext())
        {
            if(scan.next().equals(start)) 
            {
                word = start;

                while(scan.hasNext())
                {
                    String test = scan.next();
                    word = word +" "+ test;
                    if(test.equals(end))
                        break;


                }


            }
        }
    }
    catch (Exception e) 
    {
        e.printStackTrace();
    }

    return word;
}
}

4 个答案:

答案 0 :(得分:0)

只需将jar文件放入类路径中,然后在程序中导入所需的类。

答案 1 :(得分:0)

将jar文件添加到类路径中并调用其中的main()函数。

答案 2 :(得分:0)

为了使用jar,也就是说,能够在你自己的项目的类中导入外部jar的类,你必须将jar放在类路径中。将它放在类路径中归结为多个选项:

  1. 要么声明一个CLASSPATH系统环境变量,要么在那里指定一个放置外部jar的文件夹,这样编译代码时jar就可以在编译器中找到

    < / LI>
  2. 使用“classpath”标志在编译时声明包含外部jar的classpath文件夹:

    javac -classpath / path / to / dir / with / jars / MyMainClass

答案 3 :(得分:0)

一旦jar文件包含在项目lib目录中,jar中的任何类都可以被调用/导入,就好像它们是你自己的一样。您将无法编辑包含的文件,但您可以完全访问其功能。