I have Java code to open the pdf file in Java and it is running perfectly.
Code is
try {
File pdfFile = new File("/Users/alkandari/Desktop/SMART/Fahim/test.pdf");
if (pdfFile.exists()) {
if (Desktop.isDesktopSupported()) {
Desktop.getDesktop().open(pdfFile);
} else {
System.out.println("Awt Desktop is not supported!");
}
} else {
System.out.println("File is not exists!");
}
System.out.println("Done");
} catch (Exception ex) {
ex.printStackTrace();
}
However when I put this in some method say showMyPDFFile() and try to call that on commandLink, pdf doesn't get open. It says Awt Desktop is not supported!.
<h:commandLink value="View PDF/ DOC"
action="#{PersonalInformationDataBean.showMyPDFFile()}" />
Any idea why pdf is not getting opened?
So just to check, you're writing a web application, right?
The Desktop class refers to the local desktop - so it's for local GUI client applications. Even if it were supported, you'd be opening the PDF on the server's desktop - not the client's desktop, which I assume is what you're after.
It sounds like you should basically just be linking straight to the PDF file (as URL). You'll need to make the PDF available via your web server, of course.
Related
So i have a basic GUI application, where there is an option to upload image files to an ftp server. Everything works fine except one thing: the files are getting renamed during the upload. The new name of the file will be the full path of the directory, which contained the file.
So in my case, i have an image on the desktop: C:\Users\Bob\Desktop\image.png. When I select the file in the JfileChooser, the name is still just image.png. But when I click upload to FTP server, the file will be renamed to C:\Users\Bob\Desktop\image.png. So if I want to download that file, I have to use this path: /home/user/users/xy/images/C:\Users\Bob\Desktop\image.png in order to download it. Idk what causing this problem. I use FTPClient.putFileToPath(file,path) to upload the files, and it works fine, the files will be uploaded. I tried to copy a file from my machine to the ftp server with total commander, and this problem never occurred. I provided some code snippet, which does the uploading job.
uploadmenu.getUploadBtn().addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if(!uploadMenuValidate()){
for(File f : img_container){
try {
//This still gives me the normal file name
System.out.println(f.getName());
ftp.putFileToPath(f, FtpClient.DEST_DIR+SQLData.APP_USERNAME+"/"+f);
} catch (IOException ex) {
ex.printStackTrace();
}
}
popup.setVisible(false);
}
}
});
I have all the files in the img_container array that I selected in the JFileChooser.
The File.toString() returns:
Returns the pathname string of this abstract pathname
You want to use File.getName():
ftp.putFileToPath(f, FtpClient.DEST_DIR+SQLData.APP_USERNAME+"/"+f.getName());
I have a program I have written in Eclipse and it runs fine -- the HTML file opens when I run the program through Eclipse. But when I create a jar file of the program, everything else runs fine except this HTML file won't open in the browser (or anywhere):
operation.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
try {
File htmlFile = new File("help/operation.html");
Desktop.getDesktop().browse(htmlFile.toURI());
} catch (MalformedURLException MURLe) {
MURLe.printStackTrace();
} catch (IOException ioE) {
ioE.printStackTrace();
}
}
});
The rest of the program runs fine, and my images and sounds work fine and are opened, but this HTML file will not open in the menu or with the Ctrl+key shortcut. Your help is appreciated. Thanks.
When you have a file inside your jar, you cannot access it like you are doing now.
You need to read it as a stream, that's the only way.
Suppose your project is foo. Then help/operation.html will refer to
..\abc\help\operation.html
But the deployed jar file will not contain it.
You have include this operation.html file in your source code (where you write code).
Then eclipse (or any IDE) will add it into your jar file when you deploy it.
And now you can use your file as follows.
Suppose your file is present in as shown in figure.
Now you can refer your html file from any class. In this example referring it from
Accesser class.
File resFile = new File(Accesser.class.getResource("operation.html").toURI());
If you want to open your file in browser you will have to copy this file into the
user's System.
File htmlFile = new File("operation.html");
if(!htmlFile.exists) {
Files.copy(resFile.toPath(), htmlFile.toPath());
}
Desktop.getDesktop().browse(htmlFile.toURI());
Files is present in java.nio.file package
I am working on a dynamic web project.
On submit button click (present on my form)I want to create a new file and put some data inside.
I have written only these two lines and I am getting failure to create file
try{
File file = new File("C:/database.txt");
file.createNewFile();
}catch(Exception e){
return "error in creating file";
}
If I run the enire code in normal java class everything works fine. Why so?
Your web project is working on application server. The web application can manage files, which are on that server. Other files are not accessible. (of course localhost server is on your computer, but that it's path is not "C:/", so you can't write there). You can find the path of your server running this code (it also create a test file):
String pathWhereYouFindYourFile = new File("").getAbsolutePath();
System.out.println(pathWhereYouFindYourFile);
File f = new File("test.txt");
try {
f.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
I am attempting to write a JApplet that uses information in a text file to load and save data from. I have successfully got the applet to load the information, but the saving appears to be having issues. I have included the code to save below. the file name I am using is the same as I use to write to. The file must be included in the JAR when I run because the applet initializes properly. Is there any reason why the writing sin't working properly? i have resorted to calling this method from both the stop() and destroy() methods.
As a note, the load and saving both work perfectly when run from eclipse, but when in a JAR only the loading works, but nothing saves so I can't change the load data.
Ideally, I want this saveLocations() method to be called whenever the page is closed or refreshed.
NOTE: mOUtputStream is indeed a PrintWriter (it used to be an OutputStream, I guess I should change the name)
Thanks so much in advance for the help.
private void saveLocations() throws IOException {
//JOptionPane.showMessageDialog(null, "Alert", "Saving", JOptionPane.INFORMATION_MESSAGE);
// System.out.println("saving!");
try {
mOutputStream = new PrintWriter(new File(getClass().getResource("/listings/saveData.txt").toURI()));
}
catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//System.out.println(locations.size());
for (Location l : locations) {
System.out.println("r: " + l.getRawListing());
mOutputStream.print(l.getRawListing()+ "\n");
}
if (mOutputStream != null)
mOutputStream.close();
}
You can't write to a file inside a JAR file -- period. Don't even try. If you need to write to a file, then that file has to be outside the JAR. For an applet, that would require it to be signed, and to ask the user for specific permission to do so.
In the applet case, I'm not sure what copy of the JAR file you're hoping will be written to: the copy in the browser cache, or the copy on the server? Either way, it's not going to happen.
My program successfully creates and fills a Excel(.xls) file. Once created, I would like the new file to open in the system's default program (Excel in my case). How can I achieve this?
For an older program where I wanted to open a txt file in Notepad, I used the following:
if (!Desktop.isDesktopSupported()) {
System.err.println("Desktop not supported");
// use alternative (Runtime.exec)
return;
}
Desktop desktop = Desktop.getDesktop();
if (!desktop.isSupported(Desktop.Action.EDIT)) {
System.err.println("EDIT not supported");
// use alternative (Runtime.exec)
return;
}
try {
desktop.edit(new File(this.outputFilePath));
} catch (IOException ex) {
ex.printStackTrace();
}
When I try to use this code for an Excel file it gives me the following error:
java.io.IOException: Failed to edit file:C:/foo.xls
Suggestions?
Try to use Desktop.open() instead of Desktop.edit() :
Desktop dt = Desktop.getDesktop();
dt.open(new File(this.outputFilePath));
If Desktop.open() is not available then the Windows file association can be used :
Process p =
Runtime.getRuntime()
.exec("rundll32 url.dll,FileProtocolHandler " + this.outputFilePath);
You probably did the Runtime.exec incorrectly. Give this a look to see if that's the case.
If you just want to open an Excel file with Java, I'd recommend using Andy Khan's JExcel API. Perhaps using that with a Swing JTable will be just the ticket.
The most simple and efficient way.
Desktop.getDesktop().open(new File("inputFilePath"));