The Following Code is throwing the above warning:
Warning: failed to read path from javaldx
if((JButton)e.getSource()==helpButton)
{
System.out.println("-------------Help Button is pressed..----------------------");
URL helpDocument=NewCellTrenderGUILauncher.class.getClass().getResource("/G1-G2Help.doc");
System.out.println("URL Constructed-->"+helpDocument.toString());
File helpDocPath=null;
try
{
System.out.println("URI constructed from URL="+helpDocument.toURI());
helpDocPath = new File(helpDocument.toURI());
System.out.println("File Path Constructed from URI="+helpDocPath.toString());
} catch (URISyntaxException e2)
{
// TODO Auto-generated catch block
System.err.println("\n\t***********************EXCEPTION*****************");
System.err.println("\tDue to--->"+e2);
System.err.println("\tExceptionType--->"+e2.getMessage());
e2.printStackTrace();
System.err.println("\t**************************************************");
}
if(Desktop.isDesktopSupported())
{
System.out.println("DesktopSupported is TRUE..!!");
try
{
Desktop.getDesktop().open(helpDocPath);
System.out.println("FileOpened From Desktop..!!");
} catch (IOException e1)
{
// TODO Auto-generated catch block
System.err.println("\n\t***********************EXCEPTION*****************");
System.err.println("\tDue to--->"+e1);
System.err.println("\tExceptionType--->"+e1.getMessage());
e1.printStackTrace();
System.err.println("\t**************************************************");
FileRenderer.openFile(helpDocPath.toString());
System.out.println("File opened from FileReneder..!!");
}
}
else
{
System.out.println("Desptop is Not Supported..!!!");
FileRenderer.openFile(helpDocPath.toString());
System.out.println("File opened from FileReneder..!!");
}
}
The above code throws warning on the development machine,but it creates the correct path and eventually opens the file correctly. But, in case of production environment, where the platform os is Windows, it throws a null pointer exception on the following line--->
URL helpDocument=NewCellTrenderGUILauncher.class.getClass().getResource("/G1-G2Help.doc");
Please help to rectify the error..
The getResourceAsStream will do the job easily.. Try it
Related
I want to rename a csv file in java using following code segment, but file is not getting renamed.
public static void main(String[] args) {
File fileToBeRenamed = new File("C:/abc/a.txt");
File newFileName = new File("C:/abcd/b.txt");
try {
fileToBeRenamed.createNewFile();
newFileName.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
boolean isRenamed = fileToBeRenamed.renameTo(newFileName);
if(isRenamed)
System.out.println("File renamed successfully");
else
System.out.println("File could not be renamed");
}
Its not throwing any error. but file is not getting renamed.So please help me to do so.
let's suppose you have a file A(fileToBeRenamed) and you want to rename it to B(newFileName). Then , no need to create "newFileName" file. your code is fine , except the file creation part.
so comment out the lines:
try {
fileToBeRenamed.createNewFile();
newFileName.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
And it will work.
Note: I don't think it has anything to do with file extension(csv/text etc), when both are the same.
I think you want to rename a.txt to b.txt, So you don't need create b.txt. If you remove newFileName.createNewFile() will work
I am having a problem loading a custom font when I run my Java program on the command line. I receive the error messages:
Reason (IOException): Can't read Resources/Fonts/customFont.ttf
java.io.IOException: Can't read Resources/Fonts/customFont.ttf
However, when I run the program within Eclipse, the Font file is found.
The Font file is resident in the runnable Jar file created when I export my program from Eclipse.
Here is the code:
File aFile= new File("Resources/Fonts/customFont.ttf");
try {
System.out.println("About to access: " + aFile.toString());
font = Font.createFont(Font.TRUETYPE_FONT, aFile);
} catch (FontFormatException e) {
// TODO Auto-generated catch block
System.out.println("Reason (FontFormat): " + e.getMessage());
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("Reason (IOException): " + e.getMessage());
e.printStackTrace();
}
font = font.deriveFont(Font.BOLD,15);
Now, that was the latest way I've tried to get this to work from reading other people posts.
Here is the other way I've tried:
String filename="/Fonts/customFont.ttf";
Font font = null;
File aFile= new File(getURL(filename).getFile());
try {
System.out.println("About to access: " + aFile.toString());
font = Font.createFont(Font.TRUETYPE_FONT, aFile);
} catch (FontFormatException e) {
// TODO Auto-generated catch block
System.out.println("Reason (FontFormat): " + e.getMessage());
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("Reason (IOException): " + e.getMessage());
e.printStackTrace();
}
font = font.deriveFont(Font.BOLD,15);
private static URL getURL(String imageFilename) {
URL aURL= null;
aURL= Airplane.class.getResource(imageFilename);
System.out.println("Got URL: " + aURL.toString());
return aURL;
}
And got these error messages:
Got URL: rsrc:Fonts/customFont.ttf
About to access: Fonts/customFont.ttf
Reason (IOException): Can't read Fonts/customFont.ttf
java.io.IOException: Can't read Fonts/customFont.ttf
at java.awt.Font.createFont(Font.java:1008)
at Game.Screen.main(Screen.java:104)
Would appreciate any help with this!
Thanks!
I was wondering why my file is not updating to dropbox. Currently it only creates the an empty file.
final String TEST_FILE_NAME = DateTime + ".txt";
DbxPath path = new DbxPath(DbxPath.ROOT, TEST_FILE_NAME);
try {
if (!FileSystem.exists(path)) {
newFile = FileSystem.create(path);
try {
newFile.writeString("Hello world!");
} finally {
newFile.update();
newFile.close();
}
}
} catch (DbxException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Solved: I never unlinked my dbxacc and kept relinking, causing errors to show up! Silly mistake.
I am trying to use java to open an exe file. I'm not sure which program I want to open so I am using Skype as an example. When I try to do it, it gives me errors.
try {
Process p = Runtime.getRuntime().exec("C:\\Program Files (x86)\\Skype\\Phone\\Skype");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
error:
Cannot run program "C:\Program": CreateProcess error=2, The system cannot find the file specified
Try this:
String path = "/path/to/my_app.exe";
File file = new File(path);
if (! file.exists()) {
throw new IllegalArgumentException("The file " + path + " does not exist");
}
Process p = Runtime.getRuntime().exec(file.getAbsolutePath());
You have to use a string array, change to
try {
Process p = Runtime.getRuntime().exec(new String[] {"C:\\Program Files (x86)\\Notepad++\\notepad++.exe"});
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
You are on windows so you have to include the extension .exe
try {
Process p = Runtime.getRuntime().exec("C:/Program Files (x86)/Skype/Phone/Skype.exe");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Maybe use File.separator instead of '\'
I tried this and it works fine, it's taken from your example. Pay attention to the double \\
public static void main(String[] args) {
try {
Process p;
p = Runtime.getRuntime().exec("C:\\Program Files\\Java\\jdk1.8.0_05\\bin\\Jconsole.exe");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
I'm trying to open a pdf located in a ressource folder with my application.
It does work on the emulator but nothing happens when I try on the exported application.
I'm guessing I'm not using the rigth path but do not see where I'm wrong. The getRessource method works very well with my images.
Here is a code snippet :
public void openPdf(String pdf){
if (Desktop.isDesktopSupported()) {
try {
URL monUrl = this.getClass().getResource(pdf);
File myFile = new File(monUrl.toURI());
Desktop.getDesktop().open(myFile);
} catch (IOException ex) {
// no application registered for PDFs
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
I'm referring to the pdf variable this way : "name_of_the_file.pdf"
Edit: I've pasted the whole method
Ok, solved it. The file being located in a Jar, the only way to get it was through a inputsteam/outstream and creating a temp file.
Here is my final code, which works great :
public void openPdf(String pdf){
if (Desktop.isDesktopSupported())
{
InputStream jarPdf = getClass().getClassLoader().getResourceAsStream(pdf);
try {
File pdfTemp = new File("52502HPA3_ELECTRA_PLUS_Fra.pdf");
// Extraction du PDF qui se situe dans l'archive
FileOutputStream fos = new FileOutputStream(pdfTemp);
while (jarPdf.available() > 0) {
fos.write(jarPdf.read());
} // while (pdfInJar.available() > 0)
fos.close();
// Ouverture du PDF
Desktop.getDesktop().open(pdfTemp);
} // try
catch (IOException e) {
System.out.println("erreur : " + e);
} // catch (IOException e)
}
}
You mentioned that it is running on Emulator but not on the application. There is high probability that the platform on which the application is running does not support Desktop.
Desktop.isDesktopSupported()
might be returning false. Hence no stack trace or anything.
On Mac, you can do:
Runtime runtime = Runtime.getRuntime();
try {
String[] args = {"open", "/path/to/pdfFile"};
Process process = runtime.exec(args);
} catch (Exception e) {
Logger.getLogger(NoJavaController.class.getName()).log(Level.SEVERE, "", e);
}