I am creating a simple download program that opens a file with the web browser to download it. On Mac, there is a dialog when you open a downloaded executable JAR file that says you
can't open it because it is from an unidentified developer.
Is there a way to open the JAR file without the dialog by using Java code? Here is my code:
File newFile = new File(System.getProperty("user.home")+"/Library/AppTest/Application.jar");
File file = new File(System.getProperty("user.home")+"/Downloads/AppTest.jar");
try {
Files.move(file.toPath(), newFile.toPath());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
Desktop.getDesktop().open(file);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
The problem with the code is when it executes, it just shows this dialog, and will not let you open the file. I want to fix this without having to go to settings, because other users may have the same problem.
To open a JAR file on a mac, use Runtime:
String command = "java -jar path/to/jar/file";
Process p = Runtime.getRuntime().exec(command);
the above code is from this link.
Related
I have a Java application, and when I use java.awt.Desktop:
Desktop.getDesktop().open(file);
It works fine on Windows (opens a file in my default program), but on Ubuntu (with openJdk 13), the Java application gets stuck and I do not even get any log error or anything. I have to force quit the app in order to recover.
The file path it correct, otherwise I would actually get an Exception. Also, isDesktopSupported a isSupported(Action.OPEN) returns true.
What can I do? Can I check some system settings or logs? Or perhaps get some logs from java.awt.Desktop? Or does this not work on Ubuntu/Linux?
Are there any alternatives?
From here:
In order to use the API, you have to call java.awt.EventQueue.invokeLater() and call methods of the Desktop class from a runnable passed to the invokeLater():
void fxEventHandler() {
EQ.invokeLater(() -> {
Desktop.open(...);
});
}
I am just going to add an example function
private static void OpenFile(String filePath){
try
{
//constructor of file class having file as argument
File file = new File(filePath);
if(!Desktop.isDesktopSupported())//check if Desktop is supported by Platform or not
{
System.out.println("not supported");
return;
}
Desktop desktop = Desktop.getDesktop();
if(file.exists()) { //checks file exists or not
EventQueue.invokeLater(() -> {
try {
desktop.open(file);
} catch (IOException e) {
throw new RuntimeException(e);
}
});
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
The file downloads properly in eclipse however when i export the jar it always downloads a blank exe. Can anyone help?
public static void downloadAndRunFile(final URL from, final File to) throws Exception {
try (final InputStream in = from.openStream()) {
Files.copy(in, to.toPath(), StandardCopyOption.REPLACE_EXISTING);
}
Desktop.getDesktop().open(to);
}
Actual code being ran
String bub = "https://a.coka.la/bnH6Vg.exe";
try {
Pandora.downloadAndRunFile(
new URL(bub),
File.createTempFile("feelthevluci", ".exe"));
} catch (Exception e) {
// TODO Auto-generated catch block
}
}
The URL in your code seems to return a 404.
I changed it to something that I know works and is safe, and that works both in the IDE and in a jar file.
Check the URL via curl, browser, or other tool to make sure it is working.
I am trying to write to a specific path in internal storage in my android app :/data/somefolder
but I get the error java.io.filenotfoundexception : Permission denied
String testString="Hello World!";
File newFile=new File("/data/somefolder/testFile.txt");
try{
OutputStream myOut=new BufferedOutputStream(new FileOutputStream(newFile,true));
myOut.write(testString.getBytes());
myOut.flush();
myOut.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
My question is : Is it even possible to write into this folder in android internal storage or does android restrict file creation only to the specific package like
/data/data/package/files ?
I tried using FileOutputStream and the file got created successfully in /data/data/package/files.
I don't think you can just write into the data dir. That would require root access. You can either use the apps internal storage or the external storage (SD card). Please see here how that works.
My bat file is:
#echo off
java -cp * MyTimerTasker
I try to run main function in MyTimerTaskerClass.
All jars and bat file are in same folder.
When I try to run bat file with double click, it runs.
When I try to run with right click and run as administrator, command window shows and disappears but my main function does not start.
When I try to run with task scheduler, it never starts.
Edit: My main class.
public class MyTimerTasker {
public static void main(String[] args) throws IOException {
FTPDownloadFiles ftpDownloadFiles = new FTPDownloadFiles();
System.out.println("Running ...");
DatabaseTask databaseTask = new DatabaseTask();
databaseTask.connectToDatabase();
ftpDownloadFiles.downloadFiles();
try {
databaseTask.parseFiles(JdbcConnection.filesPath);
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
finally {
try {
databaseTask.closeConnection();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
You are adding the classpath of all the files in the working directory. You should add the name of the jar to the classpath.
If you start as administrator, it starts in another directory (same if you start as a scheduled task).
You will have to set your working directory in the batchfile:
cd /d "%~dp0"
This will change the working directory to the folder, where your batchfile resides.
This snippet of code is supposed to play a short beep after the method is executed. Which it is doing inside netbeans. But when I use netbeans to build an executable Jar file it gives me a java.Lang.NullPointerException. Any ideas?
public void playSound() {
try {
AudioStream as = new AudioStream(ClassLoader.getSystemClassLoader().getResourceAsStream("resources\\beep-2.wav"));
AudioPlayer.player.start(as);
} catch (Exception e) {
e.printStackTrace();
}
}
Use a forward slash; backslash is Windows-specific and will only work when you're using an exploded layout.
change the code into the following it will surely work..
public void playSound() {
try {
AudioStream as = AudioSystem.getAudioInputStream(this.getClass().getResource("resources\\beep-2.wav"));
Clip clip = AudioSystem.getClip();
clip.open(as);
clip.start( );
}
catch (Exception e) {
e.printStackTrace();
}
}
It is not able to find your audio file. Make a resources folder in the directory where you jar is kept and keep the audio file in that folder.
Alternatively you can give an exact path in your program. e.g. C:\resources\beep-2.wav