somehow I want to open a file form a web application with the desktop application from client side.
My boss told me to use Applet. I've been through all the internet could provide me, but still can't find how to do it.
I've build a code program from java class to open the file directly but I can't make the applet running from JSP file.
Here's my code :
public static void main(String[] a) {
try {
URI uri = new URI("your/local/file/path");
Desktop desktop = null;
if (Desktop.isDesktopSupported()) {
desktop = Desktop.getDesktop();
}
if (desktop != null)
desktop.browse(uri);
} catch (IOException ioe) {
ioe.printStackTrace();
} catch (URISyntaxException use) {
use.printStackTrace();
}
}
If somebody ever done it before, I'll be really thankful.
Normally, Applets do not have access to the local file system due to security issues. However, there are ways to grant file system access to applets. This article describes the procedure: https://www.developer.com/java/other/article.php/3303561/Creating-a-Trusted-Applet-with-Local-File-System-Access-Rights.htm. Altough I have not tested it myself and it is rather old, the article seems promising. I hope, this helps you.
Related
I am wondering if (and if so, how) it is possible to create new, empty text file(s) in the app directory when the app first installs. Until now I've always done a:
File cart = new File(this.getFilesDir(), "cart.txt");
if(cart.exists() == false) {
try {
cart.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
But this seems a bit impractical if I need to make about 10 empty text files and it needs to check all those 10 before it starts every time.
I was thinking maybe somewhere in the Manifest but I haven't found any online solutions.
I want to know if there is a way to open a webpage specifically in internet explorer using the java Desktop utility. My company uses both firefox and IE, but the url that needs to be open, is only compatible in IE. So, my java class needs to open that url in IE no matter what the default browser is.
Thanks for your help.
Sure:
Runtime.getRuntime().exec("iexplore.exe www.stackoverflow.com");
This might be late to answer the question, But still I will try to add my 2 cents.
The question asked is "How to open URL in internet explorer using java Desktop"
The below line of code mentioned in previous answer is correct but it will open in IE only if path is set to it. If there is not path set it will not work and throw error
Runtime.getRuntime().exec("iexplore.exe www.stackoverflow.com");
To Force Java open Webpae in IE below is the line of code worked for me even though my default browser is Edge and path for IE is "not" set.
Runtime.getRuntime().exec("C:\\Program Files\\Internet Explorer\\iexplore.exe https://carelink.minimed.eu/");
Thanks
try this:
//e.g. myURL=www.google.ch
public void openBrowser(String myURL) {
//open default OS browser
URI myURI;
try {
myURI = new URI(myURL);
Desktop my = Desktop.getDesktop();
if (!Desktop.isDesktopSupported()) {
System.out.println("[WARNING] NOT SUPPORTED");
}
my.browse(myURI);
} catch (URISyntaxException | IOException e1) {
e1.printStackTrace();
}
}
I am writing an android application that takes in a string, read in by a bar code reader, and then when a particular button press occurs, it will send that string to the database and set their values either to "in stock" or "out of stock". I have not been able to find anything on this except for jackcesss, which does not seem to have any really good documentation on it. I cannot even get it to open the file with their example code. The code looks like this:
try {
Database db = Database.open(new File("sdcard/download/Inventory-1.mdb"));
db.close();
} catch (IOException e) {
new AlertDialog.Builder(CheckInActivity.this).setTitle("CRITICAL ERROR").setMessage("DATABASE FILE NOT FOUND. Please check your wireless connection").setPositiveButton("OK",null).show();
e.printStackTrace();
}
When I run this code on my phone I get a force close (I run it on my phone each time to avoid emulator issues,also I am also try to make it work locally before I attempt to make it a remote file) However if I change the location of the file to something I know does not exist, it will catch it and pop up the dialog box that I specified. I tried this with and without closing the file, with throws and the try and catch, nothing seems to work. So what am I missing here?
How do you know the database open was unsuccessful? I'd put in an explicit indication to hit me over the head:
try {
Database db = Database.open(new File("sdcard/download/Inventory-1.mdb"));
new AlertDialog.Builder(CheckInActivity.this).setTitle("SUCCESS").setMessage("DATABASE WAS OPENED SUCCESSFULLY. ").setPositiveButton("OK",null).show(); db.close();
} catch (IOException e) {
new AlertDialog.Builder(CheckInActivity.this).setTitle("CRITICAL ERROR").setMessage("DATABASE FILE NOT FOUND. Please check your wireless connection").setPositiveButton("OK",null).show();
e.printStackTrace();
}
I have embedded an applet in an html page which access User's file directory. Its signed. When I write code inside init function then it works very well but when I write this code inside a method and call it with Javascript Then it sends me security exception. Have u any idea how can I solve this problem?
public class App extends javax.swing.JApplet {
#Override
public void init() {
}
public void callMethod(){
File file = new File("D:/test.txt");
if(!file.exists()){
try {
file.createNewFile();
} catch (IOException ex) {
Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
Javascript:
window.document.applets[0].callMethod();
See if the answers to this very similar question can help you: signed applet gives AccessControlException: access denied, when calling from javascript
To be trusted, every frame on the stack must be accounted for. Once JavaScript is in the mix, that stops being the case.
To fix it, wrap the trusted code in an AccessController.doPrivileged() method. See the JavaDocs for an example.
Is there a simple way to open a web page within a GUI's JPanel?
If not, how do you open a web page with the computer's default web browser?
I am hoping for something that I can do with under 20 lines of code, and at most would need to create one class. No reason for 20 though, just hoping for little code...
I am planning to open a guide to go with a game. The guide is online and has multiple pages, but the pages link to each other, so I am hoping I only have to call one URL with my code.
Opening a web page with the default web browser is easy:
java.awt.Desktop.getDesktop().browse(theURI);
Embedding a browser is not so easy. JEditorPane has some HTML ability (if I remember my limited Swing-knowledge correctly), but it's very limited and not suiteable for a general-purpose browser.
There are two standard ways that I know of:
The standard JEditorPane component
Desktop.getDesktop().browse(URI) to open the user's default browser (Java 6 or later)
Soon, there will also be a third:
The JWebPane component, which apparently has not yet been released
JEditorPane is very bare-bones; it doesn't handle CSS or JavaScript, and you even have to handle hyperlinks yourself. But you can embed it into your application more seamlessly than launching FireFox would be.
Here's a sample of how to use hyperlinks (assuming your documents don't use frames):
// ... initialize myEditorPane
myEditorPane.setEditable(false); // to allow it to generate HyperlinkEvents
myEditorPane.addHyperlinkListener(new HyperlinkListener() {
public void hyperlinkUpdate(HyperlinkEvent e) {
if (e.getEventType() == HyperlinkEvent.EventType.ENTERED) {
myEditorPane.setToolTipText(e.getDescription());
} else if (e.getEventType() == HyperlinkEvent.EventType.EXITED) {
myEditorPane.setToolTipText(null);
} else if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
try {
myEditorPane.setPage(e.getURL());
} catch (IOException ex) {
// handle error
ex.printStackTrace();
}
}
}
});
If you're developing an applet, you can use AppletContext.showDocument. That would be a one-liner:
getAppletContext().showDocument("http://example.com", "_blank");
If you're developing a desktop application, you might try Bare Bones Browser Launch.
haven't tried it at all, but the cobra HTML parser and viewer from the lobo browser, a browser writen in pure java, may be what you're after. They provide sample code to set up an online html viewer:
import javax.swing.*;
import org.lobobrowser.html.gui.*;
import org.lobobrowser.html.test.*;
public class BareMinimumTest {
public static void main(String[] args) throws Exception {
JFrame window = new JFrame();
HtmlPanel panel = new HtmlPanel();
window.getContentPane().add(panel);
window.setSize(600, 400);
window.setVisible(true);
new SimpleHtmlRendererContext(panel, new SimpleUserAgentContext())
.navigate("http://lobobrowser.org/browser/home.jsp");
}
}
I don't know if such a built-in exists, but I would use the Runtime class's exec with iexplore.exe or firefox.exe as an argument.
Please try below code :
import java.awt.Desktop;
import java.net.URI;
import java.net.URISyntaxExaception;
//below is the code
//whatvever the url is it has to have https://
Desktop d = Desktop.getDesktop();
try {
d.browse(new URI("http://google.com"));
} catch (IOException | URISyntaxException e2) {
e2.printStackTrace();
}