• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to open a local file in Java program

 
Greenhorn
Posts: 13
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, let's say I have a PDF file in the local machine, how can open it through Java program? When I say "open", I mean open the file and show the content to the user.

thanks,
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

Where is the Java program running, and how does it become aware of that local file?
 
Paul Hankerg
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for the response.

Here is really I want to know. On a web page, I have a button, when a user clicks this button, it will open a PDF file, and Java program know where this PDF file is. I don't want to use Java script. The open action should happen in Java program. Thanks.
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So it's a Web application, and the PDF file is already on the desktop machine. This is not the kind of scenario that Web technologies are built to deal with, unfortunately. The only way that occurs to me to do this is to have a signed Java applet on the web page; that signed applet can ask for and be granted (by the user) permission to launch an application with that file. It's a lot of work, it's messy, and users won't like granting the permission.

How does the file get on the desktop? Have you sent it previously? Could you just send it down again and let the browser handle it (maybe it's in cache?)
 
Paul Hankerg
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks. How can we call browser in Java? Actually I think it's a good idea to use broswer to open the file. But the question is how can we call brower to open the file in Java program?
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To open a browser, you would probably need to use Runtime.exec(), or JDK 5's ProcessBuilder, to start an external executable. The details will be very system-dependent, and somewhat of a pain. Or you can try the new JDK 6, now in beta, which has new features which should allow you start a browser much more easily. See this article for more info.
 
Paul Hankerg
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jim. It seems things are getting more complicated, now I need worry about how to start a browser. All I need is to open a file in Java program no matter using browser or not.
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you explain to us more generally what the problem you're solving is, maybe we can give better advice. In particular, how does the web app know about the file on the desktop machine, and how did it get where it is?
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Paul Hankerg:
Actually I think it's a good idea to use broswer to open the file.

If the file is a PDF file, then it would be a better idea to use Adobe Acrobat to open it.

But in general, Web applications aren't allowed to open files on the client machine. This is because if the file is an executable, then opening it -- i.e. executing it -- could cause it to do harmful things to the client machine. (People have even figured out how to use JPEG files to do bad things.) So your best approach, short of redesigning your system so that it doesn't include opening local files as a requirement, looks like the signed applet route.
 
Paul Hankerg
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, here is my situation.

I hava a web page, and I hava a button on it. When a user clicks this button, an action java program will create a PDF file, and put it on the local machine(So Java program knows exactly where is), then I want to open this PDF file. So one click, two things: create a PDF file and open it. I hava already created the PDF file, but I don't how to open it. I don't care what I need to use, browser or applet, all I want is to open this PDF file. I believe that deliever the job to the browser is a good idea, but the question is how to invoke it in the Java program. If you still don't understand my question, please let me know. Thanks.
 
Paul Hankerg
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It seems I don't explain "open" well. When I say "open a PDF file", it's just like we click this PDF file, and I don't care it's Adobe reader or Adobe acrobt did the job. The only difference is I want the program do the click job for me automatically.
 
Paul Hankerg
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How about the PDF file is stored in the server? I could save the file in server.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Paul Hankerg:
It seems I don't explain "open" well. When I say "open a PDF file", it's just like we click this PDF file, and I don't care it's Adobe reader or Adobe acrobt did the job. The only difference is I want the program do the click job for me automatically.



In windows... if you run "cmd /c blahblah.pdf", it will behave almost as if you clicked it. It will load whatever default reader for that filetype and run it.... So from your java program, you just need to Runtime.exec() that command.

Henry
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, I've been trying to encourage you to store the PDF on the server (or at least deliver it in real time from the server) because then all you have to do is link to it, and the browser will grab it from the server and display it. None of the other issues will come up, then.

But I'm a little confused by your description of what you've done so far, so let's back up just a bit more. The "Java action program" that has created the PDF and stored it on the desktop: what kind of a program is this, exactly? Is is a JSP or a Servlet, or is it something else? I wonder if maybe you're developing and testing all on the same machine, and you're confused about what will run on the server and what will run on the desktop when the application is deployed on the Web for real?
 
Paul Hankerg
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ernest;

I am using a set of system based on Java/JSP, which is called WDK from documentum. But my question is a pure java question. I don't want to go any further to make thing more complicated.

To answer your question, I think the java program that I am talking about is JSP. Let's say, when you click a button on a jsp page, and you want to do soemthing (in my case, create a pdf file and open it), you need write some java codes to do this, right? You could put these java codes in the web page or you could put these java codes in a java class. But somehow you need write some java codes to make it happy, right, and that's what "java action program" means.

As you could say, I created this PDF file, so I could save it anywhere I want. I guess my bad explanation complicate this things, and I truly believe this should be a very easy thing.

thanks again.
 
Paul Hankerg
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Henry;

Yes in run, it does exactly what I want. But in Java class, it doesn't open the PDF file. In my tomcat's log file, I find it print out the content of the file in my log flie.
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, Paul, great. Now we're getting somewhere!

The Java code in a JSP runs on the web server machine. If I access your application from here, your Java code can't save the PDF onto my disk -- it does not have access to my disk. It only has access to the disks on the server!

Similarly, if you were able to get Henry's suggestion to work, Acrobat would launch on your computer, not on my computer -- and that's clearly not going to help me unless I have very strong binoculars!

So in fact what you have working so far is generating the PDF and saving it on the server. In your development environment, I'm assuming the server software is running on your desktop machine, so the two separate computers I'm talking about are just accidentally the same -- which can make this very confusing, especially when you're just starting out!

So in fact what you want your button (or just a simple link) to do is request a URL from the server; and behind that URL, you need to deliver the PDF. This could be a servlet or JSP that sets the content type to application/pdf and then streams the PDF's bytes to the brower, or it could be that you save the PDF to a publically-accessible directory in the web space, and the web server just delivers the file as requested.
 
Paul Hankerg
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank, Ernest, if you don't mind, could you write some simple codes to show how to open the file. Let's say this PDF file is now in myServer's C:\backup\mydoc.PDF. How can we open it?

I tried Henry's
Runtime.getRuntime().exec("cmd /c C:\backup\mydoc.pdf");

not working.

thanks.
 
Paul Hankerg
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Ernest, if you don't mind, could you provide some simeple codes to show how to do it? Suppose my PDF file now is in server's C:\backup\mydoc.pdf. I have no problem to access it.
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Paul Hankerg:

I tried Henry's
Runtime.getRuntime().exec("cmd /c C:\backup\mydoc.pdf");

not working.



I will explain why it's not working, but it is exceedingly important that you understand why this is not the right thing to do and, even if it were working, would not "work" for the actual user.

The reason the code above is not working is that backslashes are special characters in Java strings and must be doubled to be included in a String literal; so you mean

Runtime.getRuntime().exec("cmd /c C:\\backup\\mydoc.pdf");


Now, you truly don't want to do this any way. Can you explain to me why?
 
Paul Hankerg
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ernest;

Actually I did exactly
Runtime.getRuntime().exec("cmd /c C:\\backup\\mydoc.pdf");

Not working.
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, fine, but that's not important -- as I said, it's not the right thing to do anyway and will never work in your application. Do you understand why this is?

What you want to do is have another JSP which does something like



and then your button would link to this.

I've ignored the whole concept of sessions and multiple users here; presumably you'll be looking at that soon.
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Works for me...



BTW, for this to work, Windows have to be configured to use the right reader. Is your JVM running with an account that is not configured to open PDFs correctly?

Henry
[ July 11, 2006: Message edited by: Henry Wong ]
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ernest Friedman-Hill:
OK, fine, but that's not important -- as I said, it's not the right thing to do anyway and will never work in your application. Do you understand why this is?



Sorry for stepping on EFH's point...

As EFH mentioned, the overall design is flawed. It would be better to get it working correctly, than to just get it working.

Henry
 
author
Posts: 288
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ernest Friedman-Hill:
OK, fine, but that's not important -- as I said, it's not the right thing to do anyway and will never work in your application. Do you understand why this is?

What you want to do is have another JSP which does something like



and then your button would link to this.

I've ignored the whole concept of sessions and multiple users here; presumably you'll be looking at that soon.



In a clustered web environment should use getResourceAsStream() as oppsed to using the "File".
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by ak pillai:

In a clustered web environment should use getResourceAsStream() as oppsed to using the "File".



Ummm, yeah. I think that's getting a little ahead of things here...
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To open a local file....
you can do this...

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your post Shrinivas . Looking for the similar like this.
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It might be noted that the Desktop class was added to the API since 1.6
 
reply
    Bookmark Topic Watch Topic
  • New Topic