Opening web link using only java (console) - java

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.

Related

Context-right click save link as

Using Java and Selenium, I am trying to get this link:
So from what I found, first I do a
Actions action = new Actions(driver);
scrollToElement(href);
action.contextClick(href).perform()
which brings up the menu, as it should. But then I do
action.sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).build().perform();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
}
action.sendKeys(Keys.RETURN).build().perform();
However, that seems to do an arrow down OUTSIDE of the context menu.
This is a PDF link, so instead of selecting "Save link as", it hits the down arrow OUTSIDE of the context menu, so it closes the context menu, and just left-clicks on the pdf href.
So I am wondering about somehow having it move the arrow down while still in the context box. Or is there an xpath for "Save link as..."? I can't do an inspect on it. I suppose, I could try a
//*[contains(text(), 'Save link as"')]
but not sure that will work or not? Has anyone had this situation?
You're looking into wrong direction, you should not be automating file downloads as you're not testing your application, you're testing the browser and my expectation is that it is not something you should be doing.
Moreover, when you run your test remotely, i.e. in Selenium Grid or locally in parallel mode you will face issues as the browser which is not currently in focus will send key events to the application which is in focus.
The best option is extracting link href attribute value and performing the download using OkHttp library which is under the hood of Selenium Java Client. The relevant code would be something like:
OkHttpClient client = new OkHttpClient().newBuilder().build();
Request request = new Request.Builder().url(href.getAttribute("href")).build();
Response response = client.newCall(request).execute();
File downloadedLogo = new File("myfile.pdf");
BufferedSink sink = Okio.buffer(Okio.sink(downloadedLogo));
sink.writeAll(Objects.requireNonNull(response.body()).source());
sink.close();

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 do I open a URL on the client machine using 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);

surf to a web page through console in java

I wonder is it possible to write a console application that will surf to a web page without opening a browser? I want to check if a web page is alive (and check for an HTML string in it), but I don't want to open a web browser every time for this since I have a great number of web pages to check. What is the best way to go about this? I have some knowledge in java but not so much in networking with java.
any help or direction will be greatly appreciated.
You want java.net.URL.getConnection():
InputStream is = null;
try{
URL page = new URL("http://example.com/");
URLConnection connection = page.openConnection();
is = connection.getInputStream();
}
catch(MalformedURLException e)
{
// ....
}
catch(IOException e)
{
// Couldn't connect to website
}
// do something with input stream
You might give this short tutorial a try: http://download.oracle.com/javase/tutorial/networking/urls/readingURL.html
Also you will find more code samples here: http://www.exampledepot.com/egs/java.net/pkg.html

Adobe Acrobat intercepting every URL from call to BasicService.showDocument() in java

Our In-House Java application launches various http URLs at various times, including URLs to web-pages, MS Word documents, MS Excel documents, PDF files etc.
On over 50+ machines the URL launching works fine and the correct application opens the given page/document correctly. However, on one pesky machine Adobe Acrobat is attempting to open every URL (regardless of whether the target is a pdf or not), and failing (even on pdf documents) with:
There was an error opening this document. The filename, directory name, or volume label syntax is incorrect.
The code to launch the URLs is:
URL url = new URL("http://www.example.com");
BasicService bs = (BasicService)ServiceManager.lookup("javax.jnlp.BasicService");
boolean worked = bs.showDocument(url);
The worked variable is true after the call.
Other points that may be helpful:
The application runs within Java Web-Start.
An applet running on the same machine is able to open URLs correctly using AppletContext.showDocument()
Entering a URL into the Windows "Run..." dialog launches the URL correctly.
We've reinstalled both the JRE and Adobe Acrobat.
Thanks in advance for any advice/help you can offer.
Update:
The following debug code produces the following output:
String[] services = ServiceManager.getServiceNames();
if(services!=null) {
for(int i=0;i<services.length;i++) {
System.out.println("Available Service: "+services[i]);
}
}
BasicService bs = (BasicService)ServiceManager.lookup("javax.jnlp.BasicService");
System.out.println(url);
System.out.println(bs);
System.out.println("bs.getCodeBase():"+bs.getCodeBase());
System.out.println("bs.isOffline():"+bs.isOffline());
System.out.println("bs.isWebBrowserSupported():"+bs.isWebBrowserSupported());
boolean worked = bs.showDocument(url);
System.out.println("bs.showDocument:"+worked);
} catch(UnavailableServiceException ue) {
System.out.println("UnavailableServiceException thrown");
ue.printStackTrace();
}
Available Service: javax.jnlp.BasicService
Available Service: javax.jnlp.FileOpenService
Available Service: javax.jnlp.FileSaveService
Available Service: javax.jnlp.DownloadService
Available Service: javax.jnlp.ClipboardService
Available Service: javax.jnlp.PersistenceService
Available Service: javax.jnlp.PrintService
Available Service: javax.jnlp.ExtendedService
Available Service: javax.jnlp.SingleInstanceService
http://<snip>
com.sun.jnlp.BasicServiceImpl#bbb8b5
bs.getCodeBase():http://xxx.xxxxxx.com:8080/
bs.isOffline():false
bs.isWebBrowserSupported():true
bs.showDocument:true
Have you solved this problem yet? If not, could you try the following?
FileOpenService fos = (FileOpenService)ServiceManager.lookup("javax.jnlp.FileOpenService");

Categories

Resources