Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Opening a file with a java program

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default [SOLVED] Opening a file with a java program

    Hi, beginner java programmer here

    How do I open a file with a java program? Search results return people trying to open a file with streams, or native applications and command lines and such; this is not what I want.

    What I want, is to be able to double-click, say, an image on the desktop then have it open in my java image viewer.

    I've gotten so far as to load an image from a command line argument, but that means I need to use the command prompt or a batch file for it to work...

    Thanks.
    Last edited by hafunui; March 1st, 2011 at 04:53 PM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Opening a file with a java program

    This is an OS problem as opposed to a java problem. For each OS you need to associate the file with the application, which can be relatively easy or relatively hard depending upon the OS. Alternatively, a user could define the application to open the file with using a simple right click and defining your application.

  3. #3
    Junior Member
    Join Date
    Feb 2011
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Opening a file with a java program

    I've managed to associate the .jar with a file, but the program does not launch. Here's some code.

    package simpleImageViewer;
     
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
     
    public class Main extends JFrame{
     
    	public String source = null;
    	public JPanel stage = null;
     
    	public Main(String src) {
    		source = src;
     
    		add(stage = new JPanel());
    		stage.add(new JLabel(new ImageIcon(source)));
     
    		this.pack();
    		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    		this.setLocationRelativeTo(null);
    		this.setVisible(true);
    	}
     
        public static void main(final String[] args) {
    		SwingUtilities.invokeLater(new Runnable() {
    			public void run() {
    				Main m = new Main(args[0]);
    			}
    		});
        }
     
    }

    I think, perhaps, I am making assumptions on what is being passed as the parameter to main(). I've assumed a path to the file is being passed, but this probably isn't the case, is it?

  4. #4
    Junior Member
    Join Date
    Feb 2011
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Opening a file with a java program

    Sorry to bump, but I still haven't figured this out yet.

    To reiterate, what I'm trying to do is to open a file by dragging it over MyJavaApp.jar, or in windows (MyFile > open with > MyJavaApp.jar).
    What I've assumed was to happen is that the file path is passed to the application as an argument to the main method, but my app doesn't even start up.

  5. #5
    Junior Member
    Join Date
    Feb 2011
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Opening a file with a java program

    Ok, I was looking into batch scripts and I found out I can do this:

    "C:\Program Files\Java\jre6\bin\javaw.exe" -Xms64M -Xmx4G -jar "C:\path\to\myApp.jar" %*

    I just open my file with the .bat file, which then passes it to the .jar.
    This solution is good enough for my current needs

Similar Threads

  1. locating the path to a jar file within a program
    By nemo in forum What's Wrong With My Code?
    Replies: 7
    Last Post: January 2nd, 2011, 05:41 PM
  2. [SOLVED] Search file program
    By righi in forum Java Theory & Questions
    Replies: 4
    Last Post: November 15th, 2010, 12:08 PM
  3. Opening a File for User
    By aussiemcgr in forum Java Theory & Questions
    Replies: 3
    Last Post: July 29th, 2010, 03:00 AM
  4. java program to copy a text file to onother text file
    By francoc in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: April 23rd, 2010, 03:10 PM
  5. opening Telnet Command Session
    By voyager in forum Java Networking
    Replies: 3
    Last Post: June 23rd, 2009, 10:34 AM