I 'm working on a java application and in the program I use some files such as images, a local database and an .htm file (used as help file).
I have tried to make the access to these resources, location independent. So I've made in the package that contains my source files, a subfolder named resources and I've put there all these images,etc. I access them through .getResource() method, and not by using paths. For example that's how i access an image resource:
lang_icon=new javax.swing.ImageIcon(getClass().getResource("/myeditor/resources/checked.gif"));
The problem is that when the .jar file is built, it doesn't work properly. It loads the images successfully but cannot connect to the local database or open the .htm file.
Here is the code I use to access the .htm file (it works fine when I run the application through Netbeans)
URL helpFile= getClass().getResource("/myeditor/resources/help/help.htm");
try {
BrowserLauncher launcher = new BrowserLauncher();
launcher.openURLinBrowser(helpFile.toString());
} catch (BrowserLaunchingInitializingException ex) {
Logger.getLogger(mainAppFrame.class.getName()).log(Level.SEVERE, null, ex);
} catch (UnsupportedOperatingSystemException ex) {
Logger.getLogger(mainAppFrame.class.getName()).log(Level.SEVERE, null, ex);
}
and here is the code I use to access my local database
URL url = getClass().getResource("/myeditor/resources/icddb");
database = new File(url.getFile());
try
{
Class.forName("org.hsqldb.jdbcDriver");
con = DriverManager.getConnection("jdbc:hsqldb:file:" + database+"\\icddb", "sa", "");
When I try to open the .htm file through .jar file it shows this error "This file does not have a program associated with it for performing this action. Create an association in the Folder Options in control panel".
When I try to connect to my local database it says "Severe could not reopen database".
All ideas appreciated!
Thank you and sorry for my english!
Can you print out the URL you're asking the browser to open ? That should give a clearer indication as to what's going on.
I suspect the URL you're getting from getResource() is a URL pointing to within your .jar file. As such the browser is going to have difficulty opening that. I would (perhaps) extract that file to a temporary directory and have the browser open that.
Related
In my project I'm trying to upload files to a drive in my system and when i try to access the file using browser it shows **Not allowed to load local resource: ** error. I think creating and uploading files to the project folder may solve the issue. How to do that programmatically in java?
private final String UPLOAD_DIRECTORY = "D:/dy/Stock";
File filee = new File(UPLOAD_DIRECTORY);
if (!filee.exists()) {
filee.mkdir();
}
name = new File(item.getName()).getName();
item.write(new File(UPLOAD_DIRECTORY + File.separator + name));
This is the codeI used to create and upload files to folder.
It won't. As a general rule, doing file:// anything in a browser, these days, just does not work. Why not? It's complicated, but, to oversimplify: Security.
It has nothing whatsoever to do with the access rights of the process that asks the browser to load em. The browser just will not do this.
The solution, if you want to have resources open in a browser window from a locally running app, is to have your locally running app boot up a webserver (locally), and then ask the browser to load not, say:
file:///Users/alvin/proj/myfile.txt
but to ask the browser to load, say:
http://localhost:8192/myfile.txt
... which would work, if you start a webserver on 8192.
I'm trying to build an app that gives my users right to download file from my dropbox storage.
So I've got next quesions:
1) I was following the dropbox tutorial and it says:
If the Dropbox app is installed, the SDK will switch to it so the user
doesn't have to sign in, and it will fallback to the browser if not.
If I run my app, it will open a browser with dropbox web site. Is there any way to avoid that?
2) Is it possible to download file by file name only? Can I just set url for my storage and file name without randomized url?
You can use the dropbox public folder for downloading the file that you want. You just have to put the file in that folder and then copy the url.
Then on you java you just have to put the code to download from a URL.
This is how I have done in my program and it doesn't open any browser.
A good thing about the URL in Public folder, is that it doesn't change if the filename doesn't change, so you can update your file and the URL will be the same
You can simply use client to download file by name
DbxEntry.File md;
File file = new File("destination.file");
OutputStream out = new FileOutputStream(file);
try {
md = client.getFile("/path/to/target.file", null, out);
} finally {
out.close();
}
Here null indicates that you want to receive the latest revision of file. And "/path/to/target.file" is path to file on your dropbox, like "/Public/001.jpg".
Also md can be used to retrieve some metadata about this file, like its size, name, revision, etc.
I'm developing a web application with sqlite database. While developing the application, I created the database connection in a java class. There I've mentioned my url as like below
String url = "jdbc:sqlite:D:\\Database\\profileDB.db";
I want to place the db file from that location to specific location inside my Webcontent folder say resources.
Now I want to change my URL pointing to folder file.
Webcontent/resources/profileDB.db
.
Anyone kindly suggest me how to do this. For the information, I'm not using any servlet in creating the connection, it is just a normal java class.
I dont know about the SQLite database but if you just want the url to the db file the you can use getResource("name_of _the resource") method of the Class class. it gives you the URL of the named resource. the rules for searching for the resource depends upon the classloader used to load the class.
for eg : my package(folder) structure is :
test-
|-foo.class
|-image.png
then inside foo.class
URL url = getClass().getResource("image.png");
it will give the url to the image.png
IMHO, you need to add sqlite.jar in Project Properties -> Java Build Path -> Libraries -> Add External Jar.
Then you can try connecting using:
try {
Class.forName("org.sqlite.JDBC");
connection = DriverManager.getConnection("JDBC:sqlite:sample.db");
} catch (Exception e) {
e.printStackTrace();
}
You can refer this link for sqlite jdbc
I'm creating a directory and a text file on the sdcard in one of my apps because I want to be able to move it to my computer for analysis. But I can't find the folder or the file I'm creating on my sdcard using the file browser on my computer.
I CAN find and read the file using my phones file manager but not using the file browser in windows.
So the file and folder are succesfully created and I can write to the file, I can also find and read the file using the file manager on my phone but I can't find either directory or file using my computer.
I have a uses permission for the application to allow it to write to external storage.
This is the code I use to create the file and directory.
String fileName = "testFil.txt";
String path = Environment.getExternalStorageDirectory().getAbsolutePath()+"/PulsApp";
File appDirectory = new File(path);
appDirectory.mkdirs();
File file = new File(path, fileName);
try {
file.createNewFile();
} catch (IOException e) {
}
Does anyone know what the problem is and how to fix it? I really need to be able to write files to my sdcard so I can transfer them to my computer.
I am completely baffled by this problem since all the research I've done point to that everyone else is doing the same thing.
If your device is running Android 3.0 or higher, you also need to use MediaScannerConnection to index your newly-created file before it will show up on a development PC's file explorer.
More accurately, the newly-created file needs to be indexed by the MediaStore. That will eventually happen for other reasons (e.g., device reboot). However, you are better served using scanFile() on MediaScannerConnection to get it to happen more quickly.
I blogged about this last summer.
Sometimes that the MediaScannerConnection will recognize the folder as a unknown type file, so try to create another folder inside the original one can avoid this problem.
I have met the same problem, and I use the method in the comment
And it works for me.
I want to open a PDF file from a jsp. The jsp and the PDF are in the same directory.
I am using the following piece of code:
if (Desktop.isSupported()) {
try {
File myFile = new File("<file name>.pdf");
Desktop.getDesktop().open(myFile);
} catch (IOException ex) {
// no application registered for PDFs
}
}
However, I get the error that the file is not found.
Verified user.dir and it points to my tomcat/bin.
How can I refer to the pdf to open it?
You need to specify the absolute file path. Assuming that there's a filename.pdf in the root of the public webcontent, this should do:
File myFile = new File(getServletContext().getRealPath("/filename.pdf"));
However, this construct won't work the way you'd expect. It will show the PDF file in webserver machine, not in webbrowser machine! Only when you happen to run both the webserver and webbrowser at physically the same machine, this will "work". But this does obviously not happen in real world when you publish your webapp into the internet where the webserver and webbrowser runs at physically different machines.
Instead, you just need to link to the PDF file directly.
View PDF
and let the browser handle the display.
Have you tried this? I just got this from google, so I dunno if it will work.
Process p = Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler c:\\Java- Interview.pdf");
p.waitFor();