C:\Users\Admin\Downloads\VID_20160226_203631957.mp4
when I execute above line in command prompt the corresponding video gets played with default media player.
But when I try to do same using java Runtime class it doesnt work.
I am using following method.
Runtime r= Runtime.getRuntime();
r.exec("C:\Users\Admin\Downloads\VID_20160226_203631957.mp4")
Use Desktop.open(File) which launches the associated application to open the file. Something like,
File f = new File("C:/Users/Admin/Downloads/VID_20160226_203631957.mp4");
try {
Desktop.getDesktop().open(f);
} catch (IOException e) {
e.printStackTrace();
}
You might prefer to build the path relative to the user's home directory; something like
File downloads = new File(System.getProperty("user.home"), "Downloads");
File f = new File(downloads, "VID_20160226_203631957.mp4");
Try this.
Runtime r= Runtime.getRuntime();
r.exec("cmd /c C:\\Users\\Admin\\Downloads\\VID_20160226_203631957.mp4");
Related
I have finished my Chess UI application and now want to load a chess engine to test if my UI truly is UCI-compatible. The chess engine is inside the Download folder of the Android device ('/storage/emulated/0/Download'). This is the code that is run:
try {
File f = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
String path = f.getAbsolutePath();
String stockfishPath = path + "/Stockfish-9-armv64v8";
engineProcess = Runtime.getRuntime().exec(stockfishPath);
processReader = new BufferedReader(new InputStreamReader(
engineProcess.getInputStream()));
String sCurrentLine;
ArrayList<String> output = new ArrayList<>();
while ((sCurrentLine = processReader.readLine()) != null) {
output.add(sCurrentLine);
}
processWriter = new OutputStreamWriter(
engineProcess.getOutputStream());
} catch (Exception e) {
e.printStackTrace();
}
When I run this it fails on the exec() method because it claims it cannot find the file, even though the file exists on the Android device. I tried running the "ls" command on the exec() method, but the folder inside "emulated" is empty. The obvious reason for this is probably because I do not have permission to view/access these files, but I need to know how I can do that (despite adding the READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE in the manifest file).
Is it maybe possible to embed the engine somewhere in the project (in resources?) and somehow adb-shell into that?
you cannot do that, simply because the SD card is being mounted with -noexec flag.
using the internal storage with chmod +x would be the only option available.
I need to open a video file with my code, and it works perfectly fine in Eclipse but when I export into a runnable JAR, i get an error "URI not hierarchical".
I have seen people suggest using getResourceAsStream(), but i need to have a file object as i am using Desktop.getDesktop.open(File). Can anyone help me out?
Here is the code:
try {
URI path1 = getClass().getResource("/videos/tutorialVid1.mp4").toURI();
File f = new File(path1);
Desktop.getDesktop().open(f);
} catch (Exception e) {
e.printStackTrace();
}
if it helps my folder list is like
Src
videos
videoFile.mp4
EDIT:
I plan to run this on windows only, and use launch4j to create an exe.
You can copy the file from the jar to a temporary file and open that.
Here's a method to create a temporary file for a given jar resource:
public static File createTempFile(String path) {
String[] parts = path.split("/");
File f = File.createTempFile(parts[parts.length - 1], ".tmp");
f.deleteOnExit();
try (Inputstream in = getClass().getResourceAsStream(path)) {
Files.copy(in, f.toPath(), StandardCopyOption.REPLACE_EXISTING);
}
return f;
}
And here's an example of how you'd use it:
Desktop.getDesktop().open(createTempFile("/videos/tutorialVid1.mp4"));
I wrote an updater for my Java application which downloads its newest jar-file online, replaces the shortcut to it before starting the new jar and finally deleting itself.
I used the following code to create the shortcut:
try {
//Location of shortcut -> Working
String address = "C:\\Users\\"+System.getProperty("user.name")+"\\Desktop\\App.lnk";
//Delete old shortcut -> Not working
File f = new File(address);
f.delete();
//Create new shortcut
FileWriter fw = new FileWriter(address);
fw.write("[Program]\n"); //Probably wrong section but cannot find real one
fw.write("FILE=" + (new File(App.class.getProtectionDomain().getCodeSource().getLocation().toURI()).getPath()) + "App-"+version+".jar\n"); //Shortcut to newest version
fw.flush();
fw.close();
} catch (URISyntaxException e) {e.printStackTrace();}
The code does create a file but it seems to be broken so my question is what am I doing wrong here?
This is how it works:
ShellLink shortcut = ShellLink.createLink("App.jar").setWorkingDir(new File(".").getAbsolutePath());
shortcut.getHeader().getLinkFlags().setAllowLinkToLink();
shortcut.saveTo("C:\\Users\\"+System.getProperty("user.name")+"\\Desktop\\App.lnk");
I have a problem. I try to copy a file and I get a FileNotFound exception. Here is my code:
File file = new File("C:\\.DS\\tmp\\client-" + node_id + ".war");
File dir = new File("D:\\Utils\\Apache\\Tomcat\\webapps");
try {
FileUtils.copyFileToDirectory(file, dir);
} catch (Exception e) {
e.printStackTrace();
}
And the exception is:
java.io.FileNotFoundException: Source 'C:\.DS\tmp\client-022.war' does not exist
at org.apache.commons.io.FileUtils.copyFile(FileUtils.java:1074)
at org.apache.commons.io.FileUtils.copyFileToDirectory(FileUtils.java:1013)
...
But the file is in that folder.
This code is called from JSF in Tomcat, so maybe it's a problem of Tomcat direcories. The file is generated in previous function via external command using ProcessBuilder, so maybe Java tries to parallel and the ProcessBuilder is finishing after the copying is done.
Also, in another method of the same class this code works perfectly:
File file = new File("C:\\.DS\\tmp\\client-" + node_id + ".properties");
File dir = new File("C:\\.DS\\ss\\engines");
try {
FileUtils.copyFileToDirectory(file, dir);
...
I've figured out that Java is "smart", so Process Builder runs in separate thread (or even process), and to fix my problem I have to change
ProcessBuilder pb = ...
pb.start()
to
ProcessBuilder pb = ...
Process p = pb.start()
p.waitFor()
I am having a ProcessBuilder that should delete File.txt and then rename NewFile.txt.
Problem is that both files are deleted. Any idea why and how to fix?
public class MyProcessBuilder {
public static void main(String[] args){
final ArrayList<String> command = new ArrayList<String>();
// CREATE FILES
File file = new File("File.txt");
File newFile = new File("NewFile.txt");
try{
if(!file.exists())
file.createNewFile();
if(!newFile.exists())
newFile.createNewFile();
} catch(Exception e){}
// force remove File.txt
command.add("rm");
command.add("-f");
command.add("File.txt");
// rename NewFile.txt to File.txt
command.add("mv");
command.add("NewFile.txt");
command.add("File.txt");
final ProcessBuilder builder = new ProcessBuilder(command);
try {
builder.start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
The issue is that you are running a single command, namely
rm -f File.txt mv NewFile.txt File.txt
This unconditionally deletes files named File.txt, mv and NewFile.txt.
You want to split this into two separate commands.
Better still, use File.delete() and File.renameTo(). This will not only give you more control, but will also make your code more portable.
ProcessBuilder.start creates one process. You need to call it twice because you have two commands: first with the first command and then with the second.
Incidentally, why are you not using Java's file API for this? It is a lot easier to do this from Java than to deal with the complexity of launching a separate process, not to mention more efficient.