Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 26th, 2012  |  syntax: None  |  size: 4.59 KB  |  hits: 10  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Could not open external file from jar file when it is executed from different location
  2. import java.awt.event.ActionEvent;
  3. import java.awt.event.ActionListener;
  4. import java.io.File;
  5. import java.io.IOException;
  6. import java.util.logging.Level;
  7. import java.util.logging.Logger;
  8. import javax.swing.*;      
  9.  
  10. public class help {
  11.     private static JButton jButton1;
  12.     private static void createAndShowGUI() {
  13.         //Create and set up the window.
  14.         JFrame frame = new JFrame("HelloWorldSwing");
  15.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  16.  
  17.         //Add button
  18.         jButton1 = new javax.swing.JButton("help");
  19.         // button action
  20.         jButton1.addActionListener(
  21.           new ActionListener()
  22.           {
  23.             @Override
  24.             public void actionPerformed( ActionEvent ae )
  25.             {
  26.                 try {
  27.                     File directory = new File (".");
  28.                     File html= new File("helpFile"+File.separator+"index.html");
  29.                     java.awt.Desktop.getDesktop().open(html);
  30.                     }
  31.                 catch (IOException ex) {Logger.getLogger(help.class.getName()).log(Level.SEVERE, null, ex);}
  32.             }
  33.           });
  34.         frame.getContentPane().add(jButton1);
  35.  
  36.         //Display the window.
  37.         frame.pack();
  38.         frame.setVisible(true);
  39.     }
  40.  
  41.     public static void main(String[] args) {
  42.         //Schedule a job for the event-dispatching thread:
  43.         //creating and showing this application's GUI.
  44.         javax.swing.SwingUtilities.invokeLater(new Runnable() {
  45.             public void run() {
  46.                 createAndShowGUI();
  47.             }
  48.         });
  49.     }
  50. }
  51.        
  52. java help
  53.        
  54. java -jar a.jar
  55.        
  56. [world@localhost Help]$ cd ..
  57. [world@localhost Desktop]$ java -jar Help/a.jar
  58. Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: The file: helpFile/index.html doesn't exist.
  59.     at java.awt.Desktop.checkFileValidation(Desktop.java:209)
  60.     at java.awt.Desktop.open(Desktop.java:270)
  61.     at help$1.actionPerformed(help.java:36)
  62.     at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2012)
  63.     at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2335)
  64.     at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:404)
  65.     at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
  66.     at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:253)
  67.     at java.awt.Component.processMouseEvent(Component.java:6203)
  68.     at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
  69.     at java.awt.Component.processEvent(Component.java:5968)
  70.     at java.awt.Container.processEvent(Container.java:2105)
  71.     at java.awt.Component.dispatchEventImpl(Component.java:4564)
  72.     at java.awt.Container.dispatchEventImpl(Container.java:2163)
  73.     at java.awt.Component.dispatchEvent(Component.java:4390)
  74.     at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4461)
  75.     at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4125)
  76.     at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4055)
  77.     at java.awt.Container.dispatchEventImpl(Container.java:2149)
  78.     at java.awt.Window.dispatchEventImpl(Window.java:2478)
  79.     at java.awt.Component.dispatchEvent(Component.java:4390)
  80.     at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:649)
  81.     at java.awt.EventQueue.access$000(EventQueue.java:96)
  82.     at java.awt.EventQueue$1.run(EventQueue.java:608)
  83.     at java.awt.EventQueue$1.run(EventQueue.java:606)
  84.     at java.security.AccessController.doPrivileged(Native Method)
  85.     at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:105)
  86.     at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:116)
  87.     at java.awt.EventQueue$2.run(EventQueue.java:622)
  88.     at java.awt.EventQueue$2.run(EventQueue.java:620)
  89.     at java.security.AccessController.doPrivileged(Native Method)
  90.     at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:105)
  91.     at java.awt.EventQueue.dispatchEvent(EventQueue.java:619)
  92.     at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:275)
  93.     at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:200)
  94.     at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:190)
  95.     at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:185)
  96.     at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:177)
  97.     at java.awt.EventDispatchThread.run(EventDispatchThread.java:138)
  98.        
  99. helpfile/index.html