Open a path with Desktop.open() from java on ubuntu (linux) - java

From my application written in java I want to open a folder, using the operating system file explorer.
I use Desktop.open(new File(path))
This works fine on windows, but on ubuntu 11.10 (linux) it doesn't work.
Using the Desktop.open to open a file does work, both on ubuntu and windows.
Using a step in between:
File fPath=new File(fPath)
and testing it with fPath.exists() and fPath.isDirectory() both gives true.
using the Desktop.open(new File(path)) gives me this exception:
java.io.IOException: Failed to show URI:file:/and/here/the/path/I/use/
at sun.awt.X11.XDesktopPeer.launch(Unknown Source)
at sun.awt.X11.XDesktopPeer.open(Unknown Source)
at java.awt.Desktop.open(Unknown Source)
I was not able to test this on an apple computer yet, but I hoped the Desktop.open(new File(path)) was system independent.....
by the way, the complete code:
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()) {
// show Error
return;
}
desktop = Desktop.getDesktop();
String path = "here the path ";
// by the way: I use System.getProperty("file.separator") as file seperator
try {
File fPath=new File(path);
if(!fPath.exists()){
// show Error
return;
}
if(!fPath.isDirectory()){
// show Error
return;
}
desktop.open(new File(path));
} catch (IOException e) {
log.severe(e.getMessage());
e.printStackTrace();
// show Error
return;
}
Some extra information:
OS: Linux (3.0.0-16-generic - amd64)
Java: 1.6.0_30-b12
Java home: /opt/java/64/jre1.6.0_30

I had the same problem. But in my case it was Ubuntu 18.04 and java 1.8.0_161-b12
In Windows 10, everything is working fine. But on Ubuntu
Desktop.getDesktop().open(new file)
the program stopped responding.
I decided to wrap the call in the executor:
private ExecutorService executorService;
BasicThreadFactory factory = new BasicThreadFactory.Builder()
.namingPattern("YourPatternIndeficator")
.build();
executorService = Executors.newSingleThreadExecutor(factory);
if (Desktop.isDesktopSupported()) {
File myFile = new File(path);
executorService.execute(() -> {
try {
Desktop.getDesktop().open(myFile);
} catch (IOException e) {
e.printStackTrace();
}
});
}

I was running into what sounds like the same issue on Mint 13. From what I can tell, changes to mime handling for opening directories has broken the java Desktop api. I was able to work around the problem by editing
~/.local/share/applications/defaults.list
and adding this line
x-directory/normal=nautilus.desktop
I'm running Mint 13 Cinnamon with java version "1.7.0_05"

I can't confirm the error. I took your code and constructed a main method around it, and everything works as expected. I don't exactly know where the default applications are set (in my case PCMan was opened instead of usual Nautilus, but it should fulfil its purpose in the end).
Over at java.awt.Desktop.open doesn’t work with PDF files? I have found a link pointing to an issue in Suns (Oracles) bug tracker stating that the method for opening files using AWT isn't reliable even on Windows. Maybe you should think of alternative ways of opening such applications. Furthermore AWT is deprecating soon almost for sure.
If you are utilizing SWT in your application, you could use org.eclipse.swt.program.Program.

I was running into the same issue and decided to give Java 7 a whirl. I'm running java version "1.7.0_147-icedtea" on Ubuntu 11.10_x64 and am able to open file locations in Nautilus quite happily now.

I have the same issue on my Linux Mint (and not in Windows).
That link helped me :
Troubles with java.awt.Desktop browse() method.
This seems to work on my Linux Mint-KDE.
I changed the line
Desktop.getDesktop().desktop.open(new File("/home/user/mypath"));// this throws IOException: Failed to show URI (except in Windows)
with
Desktop.getDesktop().desktop.open(new File("///home/user/mypath"));// this launches Dolphin
or with
Desktop.getDesktop().desktop.open(new File(new URI("file:///home/user/mypath").getPath()));// this launches Dolphin
Dolphin was launched with my folder "mypath". But I found no way to open a file like a pdf or txt on my Linux while it works on Windows with the first code.
(Java 1.8.0_25, Netbeans 8.02, Linux Mint 12 KDE)

I have the issue with Kubuntu 18.04 and java 11. It was solved with
sudo apt install libgnome2-0 gvfs
see https://bugs.launchpad.net/ubuntu/+source/openjdk-8/+bug/1574879/comments/5 for details. java.awt.Desktop works with Gnome not with KDE.

Related

Executing java.exe from C# in Azure Function

I'm trying to execute java to run a JAR file from an Azure function, but it seems like the java PATH variable isn't defined or something because Azure can't seem to find it. Code below:
Process proc = new Process();
try
{
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.CreateNoWindow = false;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.FileName = "java.exe";
proc.StartInfo.Arguments = "-jar file path and some more arguments";
proc.Start();
proc.WaitForExit();
if (proc.HasExited)
log.Info(proc.StandardOutput.ReadToEnd());
log.Info("Java Success!");
}
catch (Exception e)
{
log.Info("Java Fail");
log.Info(e.Message);
}
Even if I remove the proc.StartInfo.Arguments or tell it to use java.exe instead of java I still get the same error, below:
[Info] The system cannot find the file specified
Is calling java not supported in Azure functions?
OK figured this out. So far, the best way is to fully qualify the path to java.exe...
so I changed
proc.StartInfo.FileName = java.exe";
to
proc.StartInfo.FileName = "D:\\Program Files (x86)\\Java\\jdk1.8.0_73\\bin\\java.exe";
You can figure out the full path to Java using KUDU, which is https://[yourFunctionName].scm.azurewebsites.net/
If you click on Tools->DebugConsole, you can browse until you find where Java is located.
Note hard coding the path to Java is probably a bad idea so you should probably use application settings.
Edit Below is a link to a GitHub Repo with my final project. The project does some other stuff, but you can see where I call Java to execute the Batik JAR.
https://github.com/osuhomebase/SVG2PNG-AzureFunction
As of September 2022,
proc.StartInfo.FileName = java.exe";
Worked fine for me. It appears the Azure Function Environment has the JDK installed and the JAVA PATH variable set.

can not set quaqua look and feel in java

Im facing the problem in setting the quaqua look and feel and getting this error on windows seven:
Warning: ch.randelshofer.quaqua.util.Preferences failed to load Mac OS X global system preferences
java.io.FileNotFoundException: C:\Users\A.Rahman\Library\Preferences\.GlobalPreferences.plist (The system cannot find the path specified)
this is the code fro setting look and feel :
try {
UIManager.setLookAndFeel("ch.randelshofer.quaqua.QuaquaLookAndFeel");
} catch (Exception e) { e.printStackTrace();
}
it seems your lookAndFel it is supported by mac only, because it is card-coded inside of him to load a .plist ( specific for mac) even is he on windows.
You can write the windows implementation of lookandfeel
You can write to developers to write.
Doublecheck what version have you installed on windows.

JavaFX built with netbeans on my 64 bit Mac won't run on 32 bit Linux

I'm a little perplexed. I have a JavaFX program I'm working on (hobby) and it builds and runs just fine on my Mac. (I have the latest Oracle Java 7). When I run the jar file that was built on the Mac (in the latest NetBeans), on my 32-bit Linux system (also with the latest Oracle version) it gives me the following error:
java.lang.reflect.InvocationTargetException
file:/home/me/aFolder/SomeOne/Saved/Something.jar!/Something/mainWindow.fxml
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2186)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2028)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2744)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2723)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2709)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2696)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2685)
at Something.Something.start(Something.java:33)
at com.sun.javafx.application.LauncherImpl$5.run(LauncherImpl.java:319)
at com.sun.javafx.application.PlatformImpl$5.run(PlatformImpl.java:215)
at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:179)
at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:176)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl$4.run(PlatformImpl.java:176)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:76)
at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
at com.sun.glass.ui.gtk.GtkApplication$3$1.run(GtkApplication.java:82)
at java.lang.Thread.run(Unknown Source)
The line for Something.java:33 is where I call
AnchorPane mainWindowPane =
(AnchorPane)FXMLLoader.load(UdoMail.class.getResource("mainWindow.fxml"));
So, I'm rather confused as to where to start. This is a jar file built on my Mac, moved to a 32-bit Linux (running the latest 32 bit Oracle Java), and ran.
What's rather strange is that if I build this on the 32-bit Linux, then run the Linux built jar file on my Mac, I get (effectively) the same error on the Mac.
If I run on the same platform as I build, everything works just fine. (That is, if I take the code and build it on Linux it runs perfectly on Linux, but not on Mac.)
Since I can't seem to run a debugger on Linux for the Mac built jar file (and vice versa), I'm a little lost as to where to start looking. I've already ensured I'm using File.separator and looked for other OS specific stuff, but I'd like to find out what these lines in FXMLLoader.java are (in the source code) to see why it's barfing. After commenting out most of the code, the line that seems to cause the barf is a simple File instantiation:
File myFile = new File(userDirectoryString);
I use many files and directories, and this line is the first call for file or directory usage in my code. I've validated the String "userDirectoryString" and it's correct.
Any ideas?
I can help only with the notion, that you are here :
public static final String INITIALIZE_METHOD_NAME = "initialize";
// Initialize the controller
Method initializeMethod = getControllerMethods().get(INITIALIZE_METHOD_NAME);
if (initializeMethod != null) {
try {
MethodUtil.invoke(initializeMethod, controller, new Object [] {});
} catch (IllegalAccessException exception) {
// TODO Throw when Initializable is deprecated/removed
// throw new LoadException(exception);
} catch (InvocationTargetException exception) {
throw new LoadException(exception);
}
}
You can find this code in OpenJFX.
We can see, that this means, that method initialize() has thrown an exception, during its work..
You have to compile the code on the system it's supposed to run on. The JVM scans your system and writes byte code for the machine you built it on. There's nothing wrong with your code, it's how the JVM works.

exec not working with java 1.7.21, but in netbeans works fine

I made a little program and it worked fine, but now. First, it mux the xml chapter file in the mkv file, so we get a muxed mkv file. Some day ago I updated java to 1.7.21 and I think this is the problem why it is not working now. It's a little strange, but when I run in netbeans everything is fine, but when I build and I run the .jar file, it is not working. It create the xml file, but not mux in the mkv file (and because not muxed not delete the xml file). Here is the code: (filename=xml file path; mkv=mkv file path)
public void muxing() {
try {
Runtime rt = Runtime.getRuntime();
Process p = rt.exec("c:\\Program Files\\MKVtoolnix\\mkvpropedit.exe --chapters \""+filename+"\" \""+mkv+"\"");
if (p.waitFor()==0) {
File xmlfile=new File(filename);
xmlfile.delete();
}
}
catch(Exception e) {
System.out.println(e.getMessage());
}
}
The program worked with java 1.6 and I think with 1.7.17 too. Win7 32bit. Sorry for my bad English.
Oracle has made breaking changes to Runtime.exec() in Java 7 update 21 (and 6 update 45).
If the program name contains spaces, you need to specify command and arguments in an array:
Process p = Runtime.getRuntime().exec(new String[] {
"C:\\Program Files\\MKVtoolnix\\mkvpropedit.exe",
"--chapters", "\""+filename+"\"", "\""+mkv+"\""});
Another option is to use java.lang.ProcessBuilder:
Process p = new ProcessBuilder("C:\\Program Files\\MKVtoolnix\\mkvpropedit.exe",
"--chapters", "\""+filename+"\"", "\""+mkv+"\"").start();
As stated by Oracle:
Applications that need to launch programs with spaces in the program name should consider using the variants of Runtime.exec that allow the command and arguments to be specified in an array.
Alternatively, the preferred way to create operating systems processes since JDK 5.0 is using java.lang.ProcessBuilder. The ProcessBuilder class has a much more complete API for setting the environment, working directory and redirecting streams for the process.

Connecting to Quality Center v11 using COM4J

I am trying to connect to HP Quality Center V11 using Java code and com4j but i keep getting following error. Can someone please take a look at the error?
When I use the URL in my browser and log-in with same credentials, I was able to login. I double checked all the spelling of my domain, url, Id and password..
Error I get:
com4j.ComException: 800403ea (Unknown error) : Failed to Login : .\invoke.cpp:517
at com4j.Wrapper.invoke(Wrapper.java:166)
at $Proxy5.connectProjectEx(Unknown Source)
at com.testpack.TestClass.main(TestClass.java:23)
Caused by: com4j.ComException: 800403ea (Unknown error) : Failed to Login : .\invoke.cpp:517
at com4j.Native.invoke(Native Method)
at com4j.StandardComMethod.invoke(StandardComMethod.java:35)
at com4j.Wrapper$InvocationThunk.call(Wrapper.java:340)
at com4j.Task.invoke(Task.java:51)
at com4j.ComThread.run0(ComThread.java:153)
at com4j.ComThread.run(ComThread.java:134)
Code I use to connect
public static void main(String[] args) {
String url="http://XXXX/qcbin/";
String domain="ACTIVE";
String project="QC_2013_Projects";
String username="XXXX";
String password="XXXXX";
try{
ITDConnection itd=ClassFactory.createTDConnection();
itd.initConnectionEx(url);
System.out.println("Test1:"+ itd.connected());
itd.connectProjectEx(domain,project,username,password);
//System.out.println(itd.connected());
}catch(Exception e){
e.printStackTrace();
}
}
I followed these steps for getting connected to HP QC 11 from Java code using com4j on windows 7 32 bit machine
Download Com4j artefacts com4j-20120426-2.zip from https://github.com/downloads/kohsuke/com4j/com4j-20120426-2.zip
Unzip it. Open a command prompt and navigate to the unzipped folder. Then run following command to create Wrapper classes in a location CCCC with package structure as DDDD.
java -jar tlbimp.jar -o "C:\CCCC" -p "DDDD" "C:\Users\MYACC\AppData\Local\HP\ALM-Client\10\OTAClient.dll"
Now copy the OTAClient.dll and WebClient.dll from C:\Users\MYACC\AppData\Local\HP\ALM-Client\10 and save it in Windows/System32 folder.
After following the step 2, you must have a com4j-x86.dll in the location where tlbimp.jar is present. Now copy that dll to Windows/System32 folder.
Now with Admin rights, register all 3 dll files using the commands 1 by 1 as follows.
regsvr32 com4j-x86.dll
regsvr32 OTAClient.dll
regsvr32 WebClient.dll
Now create a Java Project in eclipse. In the src folder copy the DDDD folder created during the step 2. Add com4j.jar in class build path. Then have the following code in a java file to test the HP QC connection. Run the java file to check the result.
ITDConnection itd=ClassFactory.createTDConnection();
itd.initConnectionEx("http://10.10.10.10:8080/qcbin");
System.out.println(itd.connected());
itd.connectProjectEx("DOMAIN_NAME", "PROJECT_NAME", "HPQC_USERID", "HPQC_CREDENTIAL");
System.out.println(itd.projectConnected());
Hope this helps. :)
I was finally able to solve this issue. I had install HP ALM QC Client. It would install in the following path- > Your Program files->HP->HP ALM Client.
After I installed this, I was able to connect to QC.
Hope this might be useful to someone else. Thanks!
I added all the 3 .dll files in c:\Windows\SysWOW64 and executed the same code.
// This is a fairly important section for x64 bit machines,
// as a note this took me forever to figure out. Basically,
// if the DLL is not registered in the SysWOW64 dir then we
// are unable to use this as it was created when 32-bit
// computers were still all the rage. This is a quick little
// hack that registers it if it is needed to be registered.
// If it's already registered, this does nothing.
try {
Runtime.getRuntime()
.exec("C:\\windows\\SysWOW64\\regsvr32 /s lib\\OTAClient.dll")
.waitFor();
Runtime.getRuntime()
.exec("C:\\windows\\SysWOW64\\regsvr32 /s lib\\com4j-amd64.dll")
.waitFor();
Runtime.getRuntime()
.exec("C:\\windows\\SysWOW64\\regsvr32 /s lib\\com4j-x86.dll")
.waitFor();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Utilities
.showError(
new JFrame(),
"OTAClient.dll, com4j-amd64.dll or com4j-x86.dll could not "
+ "be registered, program may or may not work on a 64-bit machine "
+ "without these files. You can attempt to manually register them, "
+ "but this rarely works.");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Run this before anything else in your code, it's what I used to force any machine to register them.

Categories

Resources