I Tried to open a pdf file using a statement
Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + "Sample.pdf");.
But I have not installed a pdf reader in my system. So it when I execute this program nothing is showing. No Exceptions are comming . Any idea getting the exception if the Pdf reader is not installed in the system. If the Pdf reader is already installed this program is working perfect.
EDIT
As my previous solution was not working . Here is another one that would sure work for you:
try
{
File file = new File("Sample.pdf");
java.awt.Desktop.getDeskTop().open(file);
System.out.println("File opened successfully");
}catch(Exception ex)
{
System.out.println("Error occurred: "+ex);
}
For that you have to deal with the register key.
Use JNI to do the trick.
String productName = Advapi32Util.registryGetStringValue(
WinReg.HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion", "ProductName");
System.out.printf("Product Name: %s\n", productName);
read/write to Windows Registry using Java
Related
My app downloads a file to my Android tablet. I can see the file on my tablet, but I can't find the file using my computer's file manager.
A response to another Stack Overflow question says the file manager may need "root privileges," but I don't know how to give a file manager root privileges or download a file manager that has root privileges. Is that possible? A second answer to that same question mentions some way to transfer the file using the command line, but I haven't been able to figure out how to do that.
I need to be able to transfer the file to my computer. What is the easiest way to do this?
void newFile() {
try {
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "file.txt");
FileOutputStream fos = new FileOutputStream(file);
String myInputText = "I really hope this works!";
fos.write(myInputText.getBytes());
fos.close();
System.out.println("My file is at " + file.getAbsolutePath());
} catch (IOException e) {
e.printStackTrace();
}
}
If you familiar with Linux Operating system, that you can use command line to chmod the file privilege. But I think if you can move this file in your tablet, maybe try to transfer it by Blue tooth, email is much easier.
I am asking of there's a way how I could put like a program or a bat file or any file that has stuff written in it and them when he clicks on a button it will create that file that i have put into my project on to the users desktop is there a way?
File test = new File("C:/Users/"
+ System.getProperty("user.name")
+ "/AppData/Roaming/.minecraft/mods/welcome.txt");
try { test.createNewFile(); }
catch (IOException e1) { e1.printStackTrace(); }
this doesnt work.
If the file you want to creating is already exist in the disk, then you can print a message like the "File already exists" -
try {
File file = new File("c:\\some\location\file_name.txt");
if (file.createNewFile()){
System.out.println("File is created!");
}else{
System.out.println("File already exists.");
}
}catch (IOException e) {
e.printStackTrace();
}
In order to just put a file on an other computer you will need to be granted permission in some way to do that (otherwise anyone could just infect any networked computer with any content they desired). There are several ways you could gain access to an other computer (look into virtual private network and mapped network drive). The most common way of delivering files to another computer is to set up a web site where the user can request the file to be downloaded to their machine by clicking on a link. You could also write a client program that used an http get to allow the client user to request content be downloaded to a specific place on their machine.
ok i found out how i just downloaded the files from a server that i have "made"
Im using iText and the Document class in a JFrame to write PDFs but if i try to use the Runtime class to run it after creation i get an exception that i cant open it due to the locks still on it and if i run Unlocker on it, my JFrame has a lock token on it. How do i open the PDF if i want to write to it?
Document d = new Document();
.... code
d.close();
Runtime.getRuntime().exec("D:/PDFChartStuff.pdf");
Why does this post not meet stackoverflow's "quality standards"?
If you are using windows you can use rundll32 to launch a pdf file.
Try something like this
String pdfFile="D:/PDFChartStuff.pdf";
if (pdfFile.toString().endsWith(".pdf")) {
Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + pdfFile);
} else {
//For cross platform use
Desktop desktop = Desktop.getDesktop();
desktop.open(pdfFile);
}
I have written a java program which enabled the shared review feature on a pdf. After making the pdf as Shared Review PDF it sends the pdf to a link folder in UNIX machine (link folder means shortcut path of a folder). After that an shell script, which runs differently via crontab makes the pdf as comment enabled and via the script that pdf is copied to some folder.
Now the question is whenever I am copying the file in the UNIX link folder (for copying the file to the link folder I am using FileUtils.copyInputStream() function.), a comment enabled script is executing and doing all comment enabled things in pdf(.sh script in UNIX which runs not from my program.) but during saving the file it prompts the file is readonly and exits without saving the file. But if I do the same thing (putting the file manually to the link folder in UNIX) manually it makes the pdf file as Comment enabled. This is my sample code which sends the file to a folder.
try{
String outFname="SR_"+fname;
srEnabledIs = new FileInputStream(pdfoutputPath+fname);
if(srEnabledIs!=null && !path.equals("")){
Date today = new Date();
SimpleDateFormat format = new SimpleDateFormat("dd_MM_yyyy_hh_mm_ss_a");
String timeStamp=format.format(today);
FileUtils.copyInputStreamToFile(srEnabledIs, new File(path+outFname));
logtracker.writeDebugNormalLog("AnnotationMain","file copied to " + path);
try{
lrfile.renameTo(new File(pdfinputPath+"/pdf/"+fname.split("[.]")[0]+"_"+timeStamp+".pdf"));
}catch(Exception ex){
logtracker.writeDebugNormalLog("AnnotationMain", ex.getMessage());
ex.printStackTrace();
}
}else{
if(path.equals("")){
logtracker.writeDebugNormalLog("AnnotationMain", "File not copied as path not found.");
}
if(srEnabledIs==null){
logtracker.writeDebugNormalLog("AnnotationMain", "File not copied as InputStream is null.");
}
if(srEnabledIs==null && path.equals("")){
logtracker.writeDebugNormalLog("AnnotationMain", "File not copied as InputStream is null and path not found.");
}
}
}catch(IOException e){
logtracker.writeDebugNormalLog("AnnotationMain", e.getMessage());
e.printStackTrace();
}finally{
if(srEnabledIs!=null){
srEnabledIs.close();
}
}
But the same program is running and executing successfully in other UNIX machine. I am really unable to understand the situation. Please help me out of this situation.
I am trying to open a file i just created in my code (so i am sure that the file exists)
The code is like this:
File file = new File(filename);
file.createNewFile();
BufferedWriter bw = new BufferedWriter(new FileWriter(file));
...
bw.close();
try {
Desktop desktop = null;
if (Desktop.isDesktopSupported()) {
desktop = Desktop.getDesktop();
}
desktop.open(file);
} catch (Exception e) {
...
}
But as the title says i get a "java.io.IOException: The system cannot find the path specified" from the desktop.open(file) istruction.
The problem surely is that the file pathname contains spaces (which are translated into "%20"). Is there a way to avoid this?
I found the real problem.
It wasn't either the %20 as i supposed.
I just hadn't the privileges to directly access the file location. It's a bit complicated to explain...
i'm just sorry i coulnd't figure out the real problem before.
Thanks for your suggestions anyway!
Are you using an IDE? What is inside the variable 'filename' (it's actual contents). Line two is unnecessary.
Is the error from the stack trace pointing to BufferedWriter bw = new BufferedWriter(new FileWriter(file)); or desktop.open(file);
EDIT:
You can also try the following code
File myCSVFile; //reference to your csv file here
String execString = "excel " + myCSVFile.getAbsolutePath();
Runtime run = Runtime.getRuntime();
try {
Process pp = run.exec(execString);
} catch(Exception e) {
e.printStackTrace();
}
The java.io error is appearing because it's failing to open the file. The code above will force excel open with your file as the argument. You'll need to set your environment variable to ensure that the command 'excel' in the command line opens the Excel application.
If you're planning on releasing this application for use you can ensure that excel is installed by checking the registry, then checking the install location of Excel from there.
Try to open a different file with other applications and see if other file types are supported. As Clarisse said, IOException is thrown from the 'open' method if the specified file has no associated application or the associated application fails to be launched. If the specified file doesn't exists IllegalArgumentException is thrown, which is not in your case. If for some reason opening a CSV file with Desktop doesn't work for you, try using krslynx approach. Same can be found here. You can quickly assemble a test application for opening anything on your machine using the code found here
In the Desktop javadoc it's written :
IOException - if the specified file has no associated application or the associated application fails to be launched
So are you sure your filetype has a default application associated ?
As krslynx says, file.createNewFile() is unnecessary. However file.mkdirs() may be necessary instead, if the intermediate directories don't exist yet.
EDIT: it's not clear from your question whether this is happening in new FileWriter() or in Desktop.open(). Please clarify.