Open in default browser exception - java

I'm using Eclipse to develop Java Desktop applications. The Desktop class has a useful method called browse() which opens an URI in the system's default browser. My problem is that this feature isn't working anymore in Eclipse but still works fine outside of Eclipse e.g. when launching an executable Jar file containing the code.
Here's a short compilable example:
import java.awt.Desktop;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
public class OpenExample
{
public static void main(String[] arguments) throws URISyntaxException, IOException
{
Desktop.getDesktop().browse(new URI("https://www.google.com/"));
}
}
I'm receiving the following exception:
Exception in thread "main" java.io.IOException: Failed to open https://www.google.com/. Error message: A device attached to the system is not functioning.
at sun.awt.windows.WDesktopPeer.ShellExecute(Unknown Source)
at sun.awt.windows.WDesktopPeer.browse(Unknown Source)
at java.awt.Desktop.browse(Unknown Source)
at OpenExample.main(OpenExample.java:10)
What could be the problem? Obviously the Java code is right and since it worked before, something must be broken in Eclipse or maybe even Windows.

It might be related this this JDK bug: https://bugs.openjdk.java.net/browse/JDK-8064934
I.e. there might be a real error occurring, but the JDK is reporting the wrong error code because of this bugs.

import java.awt.Desktop;
import java.net.URI;
public class OpenExample {
public static void main(String[] args) throws Exception {
String url = "http://google.com";
if (Desktop.isDesktopSupported()) { // for windows
Desktop.getDesktop().browse(new URI(url));
} else { // for linux
Runtime runtime = Runtime.getRuntime();
runtime.exec("/usr/bin/firefox -new-window " + url);
}
}
}
try this Some times it depend upon OS as well .
Check print Statement giving URL or not .
if it will work please give your review

Related

executing r script from Java: loading libraries

I want to use rgp to do a symbolic regression task. I am executing R script from Java using ScriptEngineEx. The R code works perfectly from Rsudio. However, just as I try loading the library rgp in java, I get an error message that "there is no package called 'rgp'".
First thing I tried was using lib.loc parameter for the library() function but got the same error message of not finding rgp.
Second thing I tried was loading other libraries from the same directory as rgp (emoa (also did not work), and manipulate(was successful)). I can not understand why it worked for some packages and not for the others in the same directory
Here is my code:
import org.rosuda.jrs.ScriptEngineEx;
import javax.script.ScriptEngineManager;
import javax.script.Invocable;
import javax.script.ScriptException;
public class RScript {
public static void main(String[] args) throws NoSuchMethodException, ScriptException {
// TODO Auto-generated method stub
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngineEx engine = (ScriptEngineEx) manager.getEngineByExtension("R");
engine.eval("library(manipulate)");
engine.eval("library(emoa)");
engine.eval("library(rgp)");
engine.eval("library(rgp, lib.loc =.libPaths())");
engine.close();
}
The error message that I get:
Error in library(emoa) : there is no package called 'emoa'
Error in library(rgp) : there is no package called 'rgp'
Error in library(rgp, lib.loc = .libPaths()) :
there is no package called 'rgp'
PS:
rgp, manipulate, and emoa are installed in directory:
C:\Users\userName\Documents\R\win-library\3.1
the value of .libpaths() is:
[1] "C:/Users/userName/Documents/R/win-library/3.1"
[2] "C:/Program Files/R/R-3.1.2/library"
both stmts: library(rgp) and library(rgp, lib.loc =.libPaths()) work fine from rStuido.
I hope you can direct me to further inspection to know why loading worked for some and not other libraries thought they are installed in the same location in .libPaths(). And any hint about how to load rgp would be much appreciated.

Use of jmtp.jar to find portable devices attatched to linux OS

I want to monitor portable devices attached to my linux OS using jmtp.jar. Below is the code that i am using to do the same. It's working fine in Windows but when i run this code it gives me "Not supported OS exception". do jmtp.jar support linux OS? If not then any alternative solution for linux OS?
Code that i used
import java.io.*;
import java.math.BigInteger;
import jmtp.PortableDevice;
import jmtp.PortableDeviceManager;
import jmtp.PortableDeviceObject;
import jmtp.PortableDeviceStorageObject;
public class Jmtp {
public static void main(String[] args) {
PortableDeviceManager manager = new PortableDeviceManager();
PortableDevice device = manager.getDevices()[0];
device.open();
System.out.println(device.getModel());
System.out.println("---------------");
// Iterate over deviceObjects
for (PortableDeviceObject object : device.getRootObjects()) {
// If the object is a storage object
if (object instanceof PortableDeviceStorageObject) {
PortableDeviceStorageObject storage = (PortableDeviceStorageObject) object;
for (PortableDeviceObject o2 : storage.getChildObjects()) {
System.out.println(o2.getOriginalFileName());
}
}
}
manager.getDevices()[0].close();
}
}
Exception that i got :
Exception in thread "main" java.lang.RuntimeException: not supported os
at jmtp.PortableDeviceManager.<init>(PortableDeviceManager.java:37)
at find_usb.Jmtp.main(Jmtp.java:24)
Google states:
A project aimed at allowing java programs to access MTP compabitble portable
media players. The main target is currently windows, by wrapping the WPD api,
but design decisions have been made to support both linux and mac os x in
the future with the same API. For more information, please see the wiki.

Sigar UnsatisfiedLinkError

I'm new to Sigar. I would like to run a simple test to know how I can monitor my system.
I added sigar-1.6.4 and log4j as external libraries, but when I go to run it, I face this error:
Exception in thread "main" java.lang.UnsatisfiedLinkError: org.hyperic.sigar.Sigar.getCpuInfoList()[Lorg/hyperic/sigar/CpuInfo;
at org.hyperic.sigar.Sigar.getCpuInfoList(Native Method)
Here is my code:
import java.util.Map;
import org.hyperic.sigar.CpuInfo;
import org.hyperic.sigar.FileSystem;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException;
public class Test {
/**
* #param args
*/
public static void main(String[] args) {
Sigar sigar = new Sigar();
CpuInfo[] cpuinfo = null;
try {
cpuinfo = sigar.getCpuInfoList();
} catch (SigarException se) {
se.printStackTrace();
}
System.out.println("---------------------");
System.out.println("Sigar found " + cpuinfo.length + " CPU(s)!");
System.out.println("---------------------");
}
}
Any help would be appreciated.
I understood the problem!
I have to use the following JVM Argument:
-Djava.library.path="./lib"
in Run Configuration, Arguments tab, VM arguments in eclipse, while the contnet of sigar-bin/lib is in lib folder.
Sigar works via JNI. As such, the appropriate .so or .dll file needs to be in the path specified by the java.library.path property.
Check your sigar distribution - the zip file, I mean. Unzip it and copy the contents of
sigar-bin\lib to a location accessible by your Path, PATH, and LD_LIBRARY_PATH environment variables. Usually, only one file needs to be accessible per platform.
That should do the trick, if it doesn't, let me know and I'll see what I can do.

Error occurred during initialization of VM java/lang/NoClassDefFoundError: java/lang/ref/FinalReference

I am a student in IT and i'm still learning java and android developement.
i'm testing with some udp traffic between a desktop app and a android app.
but every time I try to run the android app it gives this error message :
Error occurred during initialization of VM
java/lang/NoClassDefFoundError: java/lang/ref/FinalReference
this is the code of the UDP client
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketAddress;
import android.app.Activity;
import android.util.Log;
public class Client extends Activity {
public static void main(String[] args) throws IOException {
try {
int bufSize = 4096;
int port = 12345;
DatagramSocket sock = new DatagramSocket(port);
sock.setReceiveBufferSize(bufSize);
byte[] buffer = new byte[bufSize];
while (true) {
DatagramPacket p = new DatagramPacket(buffer, bufSize);
sock.receive(p);
Log.d("Client", "Received: " + new String(p.getData()));
}
}finally{}
}
}
the code may contain some parts that may not work, but it gives no errors.
I just want to know why the VM won't start.
grtz
Looks like your Java SDK wasn't installed correctly.
Try and see if java is in your system PATH. You can try javac -version command on your terminal. If that produces the same error, then you need to add it to your path.
Also, this guy seems to have had the same problem as yours, check that link:
I found the solution to my error.
I used the android project like a java app, with a main function. this gave the problem of no being able to deploy the vm.
the code hade to be in the oncreate function of the activity instead of in a main function.
thanks for the help.
grtz
Try these steps in order on UBUNTU 12.04LTS:
chmod 755 eclipse.desktop
Check javac -version
Find the correct path for javac
Find correct path for JDK
Add using vi the following lines in the top of file /opt/eclipse/eclipse.ini :
-vm
/usr/bin /*if javac is in /usr/bin */
/usr/share/jdk7/usr/java/jdk1.7.0_25/bin /*if this is the path for JDK */

java.awt.Desktop.open doesn’t work with PDF files?

It looks like I cannot use Desktop.open() on PDF files regardless of location. Here's a small test program:
package com.example.bugs;
import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
public class DesktopOpenBug {
static public void main(String[] args)
{
try {
Desktop desktop = null;
// Before more Desktop API is used, first check
// whether the API is supported by this particular
// virtual machine (VM) on this particular host.
if (Desktop.isDesktopSupported()) {
desktop = Desktop.getDesktop();
for (String path : args)
{
File file = new File(path);
System.out.println("Opening "+file);
desktop.open(file);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
If I run DesktopOpenBug with arguments c:\tmp\zz1.txt c:\tmp\zz.xml c:\tmp\ss.pdf (3 files I happen to have lying around) I get this result: (the .txt and .xml files open up fine)
Opening c:\tmp\zz1.txt
Opening c:\tmp\zz.xml
Opening c:\tmp\ss.pdf
java.io.IOException: Failed to open file:/c:/tmp/ss.pdf. Error message:
The parameter is incorrect.
at sun.awt.windows.WDesktopPeer.ShellExecute(Unknown Source)
at sun.awt.windows.WDesktopPeer.open(Unknown Source)
at java.awt.Desktop.open(Unknown Source)
at com.example.bugs.DesktopOpenBug.main(DesktopOpenBug.java:21)
What the heck is going on? I'm running WinXP, I can type "c:\tmp\ss.pdf" at the command prompt and it opens up just fine.
edit: if this is an example of Sun Java bug #6764271 please help by voting for it. What a pain. >:(
I never knew about this Desktop command, untill recently through this post:
would Java's Runtime.getRuntime().exec() run on windows 7?
Previously i have been using:
Runtime.getRuntime().exec("rundll32 SHELL32.DLL,ShellExec_RunDLL "+ myfile);
And it has always worked for me. If your method does not work, may be you can think about try this command.
If you switch the order of your arugments does that cause one of the other files to get that same error. I wonder if you need to trim the end of the path before calling the File constructor.
umm...yeah ignore that... check the documentation of Desktop.open. open throws an IO exception "if the specified file has no associated application or the associated application fails to be launched " ... also from the top of the page... "The mechanism of registereing, accessing, and launching the associated application is platform-dependent. "
code for the Desktop class: http://fuseyism.com/classpath/doc/java/awt/Desktop-source.html
The open method calls DesktopPeer.open.
DesktopPeer source: http://www.jdocs.com/javase/7.b12/java/awt/peer/DesktopPeer.html
DesktopPeer is implementation specific.
Here is source for a Windows-specific implementation:
http://www.java2s.com/Open-Source/Java-Document/6.0-JDK-Platform/windows/sun/awt/windows/WDesktopPeer.java.htm
open->ShellExecute->(Native)ShellExecute
Native ShellExecute is a wrapper for Win32 ShellExecute. Here is info on the function.
http://msdn.microsoft.com/en-us/library/bb762153(VS.85).aspx
My suggestion for a work around would be to write your own implmentation of the ShellExecute function. Here is source from someone who did it. http://www.heimetli.ch/shellexec.html

Categories

Resources