aspose file tools*
The moose likes Java in General and the fly likes problem with opening pdf file on linux machine Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "problem with opening pdf file on linux machine" Watch "problem with opening pdf file on linux machine" New topic
Author

problem with opening pdf file on linux machine

Kumar Gaurav
Ranch Hand

Joined: Apr 08, 2008
Posts: 108
Hi All,

As part of my project requirement i have to write a code in java which when executed should open a pdf file (being created) on Linux machine.

I am using Runtime.exec() but not able to find correct command to open pdf file.

Please help me as it is very urgent.


Regards,
Gaurav
Carl John
Ranch Hand

Joined: Jun 14, 2010
Posts: 47



Prefer Desktop class over Runtime class...

have this snippet..

desk = java.awt.Desktop.getDesktop ();
desk.open (new java.io.File ("sample.pdf"))


CJohn
Kiaamaa Liammes
Ranch Hand

Joined: Oct 03, 2009
Posts: 32

No Urgency here


SCJP ,SCWCD
Kumar Gaurav
Ranch Hand

Joined: Apr 08, 2008
Posts: 108
It is still not working. Can any body please guide me.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

Kumar Gaurav wrote:It is still not working. Can any body please guide me.



Guide you how? You haven't told us how it's failing. If you told us the actual error message, I'm sure we could be more helpful.

Is it not finding a program that opens PDF files? Perhaps one isn't installed. xpdf, kpdf, and acroread are three possibilities. The Desktop class will only work if there's a common desktop environment set up and configured to be able to read PDF files; there's no guarantee of that.

Is it producing an error that says it can't find the file? Maybe you're using backslashes in path names, which are inappropriate on Linux.


[Jess in Action][AskingGoodQuestions]
Kumar Gaurav
Ranch Hand

Joined: Apr 08, 2008
Posts: 108
Hi Ernest Friedman,


Even on linux machine below code is not getting executed :

java.awt.Desktop desk = java.awt.Desktop.getDesktop();
if(java.awt.Desktop.isDesktopSupported())

System.out.println("Desktop supported on Linux");
else

System.out.println("Desktop not supported on Linux");

Not a single out of these two statements are getting executed.

Following error is coming

java.lang.UnsupportedClassVersionError

Any clue why it is happening. Please let me know any other information is required

Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13875
    
  10

Kumar Gaurav wrote:Following error is coming

java.lang.UnsupportedClassVersionError

Any clue why it is happening. Please let me know any other information is required

Aha, this is what we needed to know!

This error means that you are compiling your classes on a newer version of Java then you are trying to run them on. For example, you get this error when you compile your source code with JDK 6 and then try to run it on Java 5 or older.

What version of the JDK are you using and what version of Java is running on the Linux machine?


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Kumar Gaurav
Ranch Hand

Joined: Apr 08, 2008
Posts: 108
I immediately overcame java.lang.UnsupportedClassVersionError this error as there was JVM version mismatch.
I tried running following code

public class DesktopSupport {

public static void main (String[] args) {

if(java.awt.Desktop.isDesktopSupported()) {
System.out.println("Desktop supported in Env");
} else {
System.out.println("Desktop not supported in Env");
}
}
}

After that following error is coming

bash-3.2$ javac DesktopSupport.java
bash-3.2$ java DesktopSupport
Exception in thread "main" java.lang.InternalError: Can't connect to X11 window server using ':0.0' as the value of the DISPLAY variable.
at sun.awt.X11GraphicsEnvironment.initDisplay(Native Method)
at sun.awt.X11GraphicsEnvironment.access$100(X11GraphicsEnvironment.java:52)
at sun.awt.X11GraphicsEnvironment$1.run(X11GraphicsEnvironment.java:155)
at java.security.AccessController.doPrivileged(Native Method)
at sun.awt.X11GraphicsEnvironment.<clinit>(X11GraphicsEnvironment.java:131)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment(GraphicsEnvironment.java:68)
at sun.awt.X11.XToolkit.<clinit>(XToolkit.java:89)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at java.awt.Toolkit$2.run(Toolkit.java:834)
at java.security.AccessController.doPrivileged(Native Method)
at java.awt.Toolkit.getDefaultToolkit(Toolkit.java:826)
at java.awt.Desktop.isDesktopSupported(Desktop.java:151)
at DesktopSupport.main(DesktopSupport.java:7)
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19544
    
  16

You can't open a PDF file in a graphical application without a windowing session (GNOME, KDE, Xfce, Blackbox, etc) to show that application. You can try adding -Djava.awt.headless=true as a parameter to the JVM (so before the class name) but most likely that will make the operation not supported.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem with opening pdf file on linux machine
 
Similar Threads
Linux and users
help coroutine
PDF to Editable Format(RTF/HTML) & back to PDF
Redirect file from outside the web server
How to open a local file in Java program