【问题标题】:<terminated> Usage: filename error<终止> 用法:文件名错误
【发布时间】:2016-04-02 01:20:20
【问题描述】:

以下是应该能够在 Elipse 上运行的正确代码。但是我收到错误消息:“ CreatedBibData [Java Application] Usage: CreateBibData filename”。

import java.io.*;
import java.net.*;
import org.xml.sax.*;
import oracle.xml.parser.v2.SAXParser;

public class CreateBibData extends HandlerBase {
  String elementEncountered;
  String journalName, journalCode, vol, num, 
         startPage, endPage, title; 
  String authorNames[];
  int numRows;
  int numAuthors;
  static public void main(String[] argv) {
    try {
      if (argv.length != 1) {
        // Must pass in the name of the XML file.
        System.err.println("Usage: CreateBibData filename");
        System.exit(1);
      }
      // Create a new handler for the parser
      CreateBibData createData = new CreateBibData();

      // Get an instance of the parser
      Parser parser = new SAXParser();

      // Set Handlers in the parser
      parser.setDocumentHandler(createData);
      parser.setErrorHandler(createData);

      // Convert file to URL and parse
      try {
        parser.parse(createURL(argv[0]).toString());
      } catch (SAXParseException e) {
          System.out.println(e.getMessage());
        }
        catch (SAXException e) {
          System.out.println(e.getMessage());
        }  
    } catch (Exception e) {
        System.out.println(e.toString());
    }
  }

  public void startDocument() {
    System.out.println("--Start of SQL Insert Statements");
    authorNames = new String[20];
    numRows=0;
  }

  public void endDocument() throws SAXException {
    System.out.println("--End of SQL Insert Statements");
    System.out.println("Just generated "+numRows+
                       " SQL insert statements");
  }

  public void startElement(String name, AttributeList atts) 
                   throws SAXException {
    elementEncountered = name;
    if (name.equals("Authors")) {
      numAuthors=0;
    }
  }

  public void endElement(String name) throws SAXException {
    if (name.equals("Article")) {
      String aid=journalCode+":"+vol+":"+num+":"+startPage;
      System.out.println("insert into articles values ('"+ 
          aid +"','" + journalName+"',"+ vol+","+ num+","+
          startPage+","+ endPage+",'"+ title+"');");
      numRows++;
      for (int i=0; i<numAuthors; i++) {
        System.out.println("insert into authors values('"+
            aid+"','"+authorNames[i]+"');");
        numRows++;
      }
      numAuthors=0;
    }
  }

  public void characters(char[] cbuf, int start, int len) {
    if (elementEncountered.equals("JournalName")) {
      journalName = new String(cbuf,start,len);
      int ii = journalName.indexOf("(");
      int jj = journalName.indexOf(")");
      journalCode=journalName.substring(ii+1,jj);
    }
    else if (elementEncountered.equals("Volume")) {
      vol=new String(cbuf,start,len);
    }
    else if (elementEncountered.equals("Number")) {
      num=new String(cbuf,start,len);
    }
    else if (elementEncountered.equals("Title")) {
      title=new String(cbuf,start,len);
    }
    else if (elementEncountered.equals("StartPage")) {
      startPage=new String(cbuf,start,len);
    }
    else if (elementEncountered.equals("EndPage")) {
      endPage=new String(cbuf,start,len);
    }
    else if (elementEncountered.equals("Title")) {
      title=new String(cbuf,start,len);
    }
    else if (elementEncountered.equals("Author")) {
      authorNames[numAuthors++]=new String(cbuf,start,len);
    }
   elementEncountered = "";
  }

  public void warning (SAXParseException e)
              throws SAXException {
    System.out.println("Warning:"+e.getMessage());
  }

  public void error (SAXParseException e)
              throws SAXException {
    throw new SAXException(e.getMessage());
  }

  public void fatalError (SAXParseException e)
              throws SAXException {
    System.out.println("Fatal error");
    throw new SAXException(e.getMessage());
  }

  static URL createURL(String fileName) {
    URL url = null;
    try {
      url = new URL(fileName);
    } catch (MalformedURLException ex) {
        File f = new File(fileName);
        try {
          String path = f.getAbsolutePath();
          String fs = System.getProperty("file.separator");
          if (fs.length() == 1) {
            char sep = fs.charAt(0);
            if (sep != '/')
              path = path.replace(sep, '/');
            if (path.charAt(0) != '/')
              path = '/' + path;
          }
          path = "file://" + path;
          url = new URL(path);
        } catch (MalformedURLException e) {
            System.out.println("Cannot create url for: "+ 
                               fileName);
            System.exit(0);
          }
      }
    return url;
  }
}

有人知道这是为什么吗?

【问题讨论】:

  • 听起来代码不是你写的。如果你读过 main() 方法,你就会知道如果你没有传递一个参数,程序就会抛出这个错误。
  • 是的,你是对的。代码不是我写的,它会引发异常。如何正确设置我的环境,使其不会引发此异常?谢谢。
  • 我在下面添加了一个答案。

标签: java xml xml-parsing


【解决方案1】:

从菜单中转到运行->运行配置。然后在参数框中输入文件名的完整路径,然后单击“运行”按钮。如果文件位于项目的根级别,您也可以简单地给出文件名。

通过这样做,您将参数发送到您的 main 方法(该方法接受 String[] 作为输入)。

【讨论】:

  • 在这个程序参数中,我想我应该把与这个程序相关的xml文件放进去。但是,我尝试了路径,它仍然给了我相同的异常消息。
  • 你可以创建一个运行配置的屏幕截图并上传到imgur.com 吗?
  • 看起来不错,您在同一个窗口中单击了“运行”按钮?如果该文件不存在于该位置,您至少应该会看到不同的错误。
  • 这是我现在得到的错误:java.io.FileNotFoundException: C:\Users\dd\workspace\SAXProcessing\bib.xml(系统找不到指定的文件)
  • 好的。那么您可以将文件放在该位置并重新运行程序吗?或者你可以把它放在其他地方(比如 C:) 并在参数窗口中给出完整的路径。
猜你喜欢
  • 2013-07-24
  • 1970-01-01
  • 1970-01-01
  • 2014-10-23
  • 1970-01-01
  • 2014-12-03
  • 1970-01-01
  • 1970-01-01
  • 2021-11-28
相关资源
最近更新 更多