How do I open a URL on the client machine using java - java

I am trying to open a static url from a web application when user clicks a button on a screen. Our application is deployed on a linux box and using the below program its trying to open a browser. Can you please advise how I can get it to to open it on the client instead ?
All our users access this application from windows.
try {
Desktop desktop = java.awt.Desktop.getDesktop();
URI oURL = new URI("http://www.google.com");
desktop.browse(oURL);
} catch (Exception e) {
e.printStackTrace();
}

I'm 99% sure above solution works only for windows, for unix I believe you should try something like this:
Runtime runtime = Runtime.getRuntime();
runtime.exec("/usr/bin/firefox -new-window " + url);

Related

Java (Spring Boot) download file from FTP "Changing source IP is not permitted by Firebox policy"

I'm trying to download a XML file using apache commons net 3.8.0 in Java Spring Boot.
FTPClient client = new FTPClient();
try (OutputStream os = new FileOutputStream(fileName)) {
client.connect(url);
boolean login = client.login(username, password);
if (login) {
System.out.println("Login success...");
// Download file from FTP server.
boolean status = client.retrieveFile(fileName, os);
System.out.println("status = " + status);
System.out.println("reply = " + client.getReplyString());
}
} catch (IOException exception) {
exception.printStackTrace();
}
This code works perfectly fine on my localhost. But when I deploy it, I get:
"550 Permission denied. (Changing source IP is not permitted by Firebox policy)"
The login seems to work just fine as it prints "Login success"
The deployed version is containerized using docker, the container is not exposed to public and is only reachable through a reverseproxy (caddy). The FTP server is not from me so I cannot change any settings there.
Any idea how to fix this error?
Entering the FTP using passiveMode was the trick

Opening web link using only java (console)

Is there an option how to open a link using only console so for instance, If I enter 1 it will open a separate window where it will open my web link? I mean I want to use external links (URLs) where when I press 1 a chrome window will open with my URL. Is that possible and if so how, because I have only seen people do it using Java Swing. Any help would be great :)
Ok so I tried and this worked:
try {
Desktop desktop = java.awt.Desktop.getDesktop();
URI oURL = new URI("http://www.google.com");
desktop.browse(oURL);
} catch (Exception e) {
e.printStackTrace();
}
You will also need to implement URI and java.awt library
Your question is not clear enough to me, but what I understood is that you want to open the website link from the browser console, if that's what you mean you can do it using this javascript code
For external URLs :
window.location = 'http (s): // www.example.com'
and for the internal URLs :
__ window.location = '/ file.php'
or window.location = '/ file.html'
or window.location = '/ your_route'
As I understand your description you want to open a web link based on an input (1 in your case) in your Java console app. You can use the method exec as the follownig:
try {
Runtime.getRuntime().exec("explorer https://www.google.com");
} catch(Exception e) {
e.printStackTrace();
}
here the exec method calls a command which here I call Google Chrome to open Google.com.
Using this in Windows will open the default browser showing the website.

Using java to open a web browser on another computer

So, this is a rather unusual question, and I can't find anything else anywhere which has been helpful on how to do this, or if its even possible to do so.
I'm working on a game server wrote in java, and I'm trying to get the users default web browser to open to a specific link, when a command is typed into the chat box and sent to the server.
The current Issue I have is, when a user issues the command, it opens the browser on the host system, and not the players system.
I haven't been able to try any other methods, as I am unable to find any information regarding my specific situation!
#CommandHandlerMethod(accessLevel = EAccessLevel.USER)
public static Object[] vote(final Player player, final String... params) {
try {
Desktop desktop = java.awt.Desktop.getDesktop();
URI oURL = new URI("www.example.com");
desktop.browse(oURL);
} catch (Exception e) {
e.printStackTrace();
}
return AbstractCommandHandler.getAcceptResult("");
}
What I was hoping for via this code, was to open the web browser on the players system to allow them to view a specific webpage, but this has not been the case, and opens it on the server host system.

How can i navigate (e.g. open Mozilla) in a Remote Desktop by using java code written in eclipse?

I want to be able to open Mozilla Firefox in a Remote Desktop and be able to "play" inside Mozilla (accessing URLs, import from the "Desktop" of the Remote Desktop Connection). All these, I want them to be automatically executed. That is why I have created a java code in eclipse. So far, I am able to automatically access the Remote Desktop Connection. Please find the code that I am using for this:
public static void main(String[] args) throws IOException, InterruptedException {
String ip = "xxx.xxx.xx.xx"; //check if this is the desired IP
String userName = "XXX"; //check if this is the correct username
String password = "XXXxxx"; //check if the password has changed
String jacobDllVersionToUse;
jacobDllVersionToUse = "jacob-1.18-x64.dll";
File file = new File("lib", jacobDllVersionToUse);
System.setProperty(LibraryLoader.JACOB_DLL_PATH, file.getAbsolutePath());
// creating credentials
Process p = Runtime.getRuntime().exec("cmdkey /generic:"+ip+" /user:"+userName+" /pass:"+password );
p.destroy();
Process p2 = Runtime.getRuntime().exec("mstsc /v: "+ip+" /f /console");
AutoItX x = new AutoItX();
x.winWaitActive("Remote Desktop Connection");
x.controlClick("Remote Desktop Connection", "Co&nnect", "1");
x.winWaitActive("Remote Desktop Connection");
x.controlClick("Remote Desktop Connection", "Co&nnect", "1");
}
Could you assist me on how I will be able to open Mozilla in Remote Desktop through eclispse?
Thank you in advance!
I assume the code successfully activates the RDP Window.
Once it is active AutoIt can't identify elements controls inside the window, but you can still send keyboard commands using Java Robot or even AutoIt.
To launch Firefox send a Window+R and then pass the path of firefox.exe ("C:\Program Files\Mozilla Firefox\firefox.exe") and send ENTER.

.msi file launched with java closes after a few seconds.

I am trying to launch MySql server installer which is in my resources folder but it terminates after a few seconds. However if I launch it manually it runs okay until the end. Below is my code.
Thread t = new Thread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
try {
ClassLoader classloader = Thread.currentThread().getContextClassLoader();
String fileUrl = classloader.getResource("mysql.msi").getFile();
Runtime rf = Runtime.getRuntime();
Process pf = rf.exec("msiexec /i \"\\" + fileUrl + "\"");
} catch (Exception e) {
// System.out.println(e.toString()); // not necessary
e.printStackTrace();
}
}
});
t.start();
Okay, it was just an advice, lets come to your case, Windows OS has certain set of security restrictions which allows only administrator to install or remove any application.
That is why, we see a promt window asking for Administrator password (or Admin's permission as YES/NO type, in case user has logged in as admin), and the promt screen is the heart of it's security, as it don't allow ANY OTHER APPLICATION TO HAVE CONTROL ON IT.
If you do a remote desktop via third party, you will never see the client machines promt screen (this is because of security constraints), so in your case, your java application is third party app which don't have enough permission to continue the operation further.
Hence it closes after few seconds.
How ever, you can start and stop already installed services by allowing permission once in your windows service control. So I was suggesting you to play with service only.

Categories

Resources