I am trying to use java to open an exe file. I'm not sure which program I want to open so I am using Skype as an example. When I try to do it, it gives me errors.
try {
Process p = Runtime.getRuntime().exec("C:\\Program Files (x86)\\Skype\\Phone\\Skype");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
error:
Cannot run program "C:\Program": CreateProcess error=2, The system cannot find the file specified
Try this:
String path = "/path/to/my_app.exe";
File file = new File(path);
if (! file.exists()) {
throw new IllegalArgumentException("The file " + path + " does not exist");
}
Process p = Runtime.getRuntime().exec(file.getAbsolutePath());
You have to use a string array, change to
try {
Process p = Runtime.getRuntime().exec(new String[] {"C:\\Program Files (x86)\\Notepad++\\notepad++.exe"});
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
You are on windows so you have to include the extension .exe
try {
Process p = Runtime.getRuntime().exec("C:/Program Files (x86)/Skype/Phone/Skype.exe");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Maybe use File.separator instead of '\'
I tried this and it works fine, it's taken from your example. Pay attention to the double \\
public static void main(String[] args) {
try {
Process p;
p = Runtime.getRuntime().exec("C:\\Program Files\\Java\\jdk1.8.0_05\\bin\\Jconsole.exe");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Related
i want to copy directories from the the place where my .jar files exist?
here are what i tried.. but i always get /home/user/
how can i copy files from where my .jar program exist?
private void copy_dir() {
//Path sourceParentFolder = Paths.get(System.getProperty("user.dir") + "/Project/");
// Path sourceParentFolder = Paths.get(Paths.get(".").toAbsolutePath().normalize().toString());
Path destinationParentFolder = Paths.get(System.getProperty("user.home"));
try {
Stream<Path> allFilesPathStream = Files.walk(sourceParentFolder);
Consumer<? super Path> action = new Consumer<Path>() {
#Override
public void accept(Path t) {
try {
String destinationPath = t.toString().replaceAll(sourceParentFolder.toString(), destinationParentFolder.toString());
Files.copy(t, Paths.get(destinationPath));
} catch (FileAlreadyExistsException e) {
//TODO do acc to business needs
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
allFilesPathStream.forEach(action);
} catch (FileAlreadyExistsException e) {
//file already exists and unable to copy
} catch (IOException e) {
//permission issue
e.printStackTrace();
}
}
Try
Path destinationParentFolder = Paths.get(System.getProperty("user.dir"));
"user.dir" gets the absolute path from where your application was initialized.
"user.home" gets the user's home directory.
I am having a problem loading a custom font when I run my Java program on the command line. I receive the error messages:
Reason (IOException): Can't read Resources/Fonts/customFont.ttf
java.io.IOException: Can't read Resources/Fonts/customFont.ttf
However, when I run the program within Eclipse, the Font file is found.
The Font file is resident in the runnable Jar file created when I export my program from Eclipse.
Here is the code:
File aFile= new File("Resources/Fonts/customFont.ttf");
try {
System.out.println("About to access: " + aFile.toString());
font = Font.createFont(Font.TRUETYPE_FONT, aFile);
} catch (FontFormatException e) {
// TODO Auto-generated catch block
System.out.println("Reason (FontFormat): " + e.getMessage());
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("Reason (IOException): " + e.getMessage());
e.printStackTrace();
}
font = font.deriveFont(Font.BOLD,15);
Now, that was the latest way I've tried to get this to work from reading other people posts.
Here is the other way I've tried:
String filename="/Fonts/customFont.ttf";
Font font = null;
File aFile= new File(getURL(filename).getFile());
try {
System.out.println("About to access: " + aFile.toString());
font = Font.createFont(Font.TRUETYPE_FONT, aFile);
} catch (FontFormatException e) {
// TODO Auto-generated catch block
System.out.println("Reason (FontFormat): " + e.getMessage());
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("Reason (IOException): " + e.getMessage());
e.printStackTrace();
}
font = font.deriveFont(Font.BOLD,15);
private static URL getURL(String imageFilename) {
URL aURL= null;
aURL= Airplane.class.getResource(imageFilename);
System.out.println("Got URL: " + aURL.toString());
return aURL;
}
And got these error messages:
Got URL: rsrc:Fonts/customFont.ttf
About to access: Fonts/customFont.ttf
Reason (IOException): Can't read Fonts/customFont.ttf
java.io.IOException: Can't read Fonts/customFont.ttf
at java.awt.Font.createFont(Font.java:1008)
at Game.Screen.main(Screen.java:104)
Would appreciate any help with this!
Thanks!
I was wondering why my file is not updating to dropbox. Currently it only creates the an empty file.
final String TEST_FILE_NAME = DateTime + ".txt";
DbxPath path = new DbxPath(DbxPath.ROOT, TEST_FILE_NAME);
try {
if (!FileSystem.exists(path)) {
newFile = FileSystem.create(path);
try {
newFile.writeString("Hello world!");
} finally {
newFile.update();
newFile.close();
}
}
} catch (DbxException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Solved: I never unlinked my dbxacc and kept relinking, causing errors to show up! Silly mistake.
I'm trying to open a pdf located in a ressource folder with my application.
It does work on the emulator but nothing happens when I try on the exported application.
I'm guessing I'm not using the rigth path but do not see where I'm wrong. The getRessource method works very well with my images.
Here is a code snippet :
public void openPdf(String pdf){
if (Desktop.isDesktopSupported()) {
try {
URL monUrl = this.getClass().getResource(pdf);
File myFile = new File(monUrl.toURI());
Desktop.getDesktop().open(myFile);
} catch (IOException ex) {
// no application registered for PDFs
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
I'm referring to the pdf variable this way : "name_of_the_file.pdf"
Edit: I've pasted the whole method
Ok, solved it. The file being located in a Jar, the only way to get it was through a inputsteam/outstream and creating a temp file.
Here is my final code, which works great :
public void openPdf(String pdf){
if (Desktop.isDesktopSupported())
{
InputStream jarPdf = getClass().getClassLoader().getResourceAsStream(pdf);
try {
File pdfTemp = new File("52502HPA3_ELECTRA_PLUS_Fra.pdf");
// Extraction du PDF qui se situe dans l'archive
FileOutputStream fos = new FileOutputStream(pdfTemp);
while (jarPdf.available() > 0) {
fos.write(jarPdf.read());
} // while (pdfInJar.available() > 0)
fos.close();
// Ouverture du PDF
Desktop.getDesktop().open(pdfTemp);
} // try
catch (IOException e) {
System.out.println("erreur : " + e);
} // catch (IOException e)
}
}
You mentioned that it is running on Emulator but not on the application. There is high probability that the platform on which the application is running does not support Desktop.
Desktop.isDesktopSupported()
might be returning false. Hence no stack trace or anything.
On Mac, you can do:
Runtime runtime = Runtime.getRuntime();
try {
String[] args = {"open", "/path/to/pdfFile"};
Process process = runtime.exec(args);
} catch (Exception e) {
Logger.getLogger(NoJavaController.class.getName()).log(Level.SEVERE, "", e);
}
I used the following code to run an exe I load through my code.
private static String filelocation = "";
.
load_exe.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
try {
JFileChooser file_Choose = new JFileChooser();
file_Choose.showOpenDialog(frame);
JavaSamp.filelocation = file_Choose.getCurrentDirectory()
.toString()
+ "\\" + file_Choose.getSelectedFile().getName();
System.out.println("FileLocation" + JavaSamp.filelocation);
} catch (Exception expobj) {
// TODO Auto-generated catch block
}
Runtime rt = Runtime.getRuntime();
try {
System.out.println("File Run Location" + JavaSamp.filelocation);
proc = rt.exec(JavaSamp.filelocation);
} catch (IOException e4) {
e4.printStackTrace();
} catch (Exception e2) {
}
}
});
My problem is, the above execution of the JavaSamp.filelocation, should have to done many times. First time only I load the exe. Next time I wont. I need to store the exe in a string to run for the successive times.
Any suggestion pls
If you want remember the used file just initialize the filelocation with null and test for it. BTW: Storing it as File makes more sense and your way of constructing the absolute path is a bit intricate - compared to just calling getAbsolutePath()
private static File filelocation = null;
private static void test() {
load_exe.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// Check if file-name to execute has already been set
if (filelocation != null) {
try {
JFileChooser file_Choose = new JFileChooser();
file_Choose.showOpenDialog(frame);
JavaSamp.filelocation = file_Choose.getSelectedFile();
System.out.println("FileLocation"
+ JavaSamp.filelocation.getAbsolutePath());
} catch (Exception expobj) {
}
}
Runtime rt = Runtime.getRuntime();
try {
System.out.println("File Run Location"
+ JavaSamp.filelocation.getAbsolutePath());
Process proc = rt.exec(JavaSamp.filelocation
.getAbsolutePath());
} catch (IOException e4) {
e4.printStackTrace();
}
}
};
}