我不是一名职业程序员(我所拥有的任何 Java 知识都来自 Hard Knocks 学院)。请原谅我提出的愚蠢问题,并妥善回答。
我正在开发的一个 Java 应用程序使用了非常有缺陷的与平台无关的通知(例如文件已成功下载时)。我想使用平台感知通知。在 Linux 上发出通知的代码非常简单:
import org.gnome.gtk.Gtk;
import org.gnome.notify.Notify;
import org.gnome.notify.Notification;
public class HelloWorld
{
public static void main(String[] args) {
Gtk.init(args);
Notify.init("Hello world");
Notification Hello = new Notification("Hello world!", "This is an example notification.", "dialog-information");
Hello.show();
}
}
Run Code Online (Sandbox Code Playgroud)
在 Mac 上,它有点复杂,但仍然可行:
interface NsUserNotificationsBridge extends Library {
NsUserNotificationsBridge instance = (NsUserNotificationsBridge)
Native.loadLibrary("/usr/local/lib/NsUserNotificationsBridge.dylib", NsUserNotificationsBridge.class);
public int sendNotification(String title, String subtitle, String text, int timeoffset);
}
Run Code Online (Sandbox Code Playgroud)
它需要从这个 github 存储库获取 dylib:https://github.com/petesh/OSxNotificationCenter
Windows方式是这样的:
import java.awt.*;
import java.awt.TrayIcon.MessageType; …Run Code Online (Sandbox Code Playgroud) 我想在我的java程序中打开一个文件,但我无法让它工作.我希望我的程序可以在所有平台上运行,尤其是在ubuntu中.
这是我有的:
private void openFileChoser() {
JFileChooser chooser = new JFileChooser(".");
int returnVal = chooser.showOpenDialog(getParent());
if(returnVal == JFileChooser.APPROVE_OPTION) {
openFile(chooser.getSelectedFile().getAbsolutePath());
} else {
System.out.println("Aucun fichier choisi");
}
}
/***
* ouverture du fichier :
* @param name
*/
private void openFile(String name) {
System.out.println("Ouverture du fichier: " +
name);
try {
Runtime.getRuntime().exec(new String[]{name});
//Desktop.getDesktop().open(new File(name)); //doesn't work on ubuntu 13.04
} catch (Exception e) {
System.err.println("Erreur d'ouverture du fichier : "+name);
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
我试图打开文件:/home/user/IdeaProjects/androidresourcehelper/test.xls
我得到了例外:
java.io.IOException: Cannot run program …Run Code Online (Sandbox Code Playgroud) 有没有办法在Java中告诉程序是在Windows或Linux机器上运行?我有一个.jar文件,我想在这两个文件上运行.
我写了一个java程序,我访问了不同的服务器并访问了一些文件.目前我只访问Linux服务器并执行一些命令.实际上这现在非常特定于linux环境.我希望当我访问服务器时,我的程序将检查天气是Linux环境还是Windows.如果是windows环境,它将执行命令,或者如果它是Linux环境,它将执行Linux命令.如何以务实的方式检查平台.