Win a copy of Modern JavaScript for the Impatient this week in the Server-Side JavaScript and NodeJS forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

problem with opening pdf file on linux machine

 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Ranch Hand
Posts: 47
Netbeans IDE Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Prefer Desktop class over Runtime class...

have this snippet..

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

 
Ranch Hand
Posts: 32
Eclipse IDE Objective C Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No Urgency here
 
Kumar Gaurav
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is still not working. Can any body please guide me.
 
author and iconoclast
Posts: 24203
43
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Kumar Gaurav
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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?
 
Kumar Gaurav
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
Sheriff
Posts: 21997
107
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Skool. Stay in. Smartness. Tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic