I'm trying to open a new DevCPP file here. But I want to open it from Java itself and not having to go click on "New" in DevCPP.
This is what I've tried using Runtime object
Runtime runtime = Runtime.getRuntime();
String[] s = new String[] {"C:\\Program Files (x86)\\Dev-Cpp\\devcpp.exe"};
try {
runtime.exec(s);
} catch (IOException e1) {
e1.printStackTrace();
}
I want to open the devcpp.exe to a page where a new File has been created
The above program works fine for opening Dev but does not create a new file by itself.
P.S : Thanks in advance
Although I don't have DevCpp installed (no admin rights on my PC), this should do the trick:
String[] s = new String[] {"C:\\Program Files (x86)\\Dev-Cpp\\devcpp.exe", "new_file.cpp"};
If I understand your question correctly, you want to open notepad for an already existing file using java program, then the command will be like this.
notepad
An example is given below.
notepad C:\Users\Name\Desktop\Temp-Movies.txt
Use the above as command in the java program.
Before executing java program, first run using command prompt.
Related
File file = new File ("D:\\Folder\\Folder2\\");
Desktop desktop = Desktop.getDesktop();
try {
desktop.open(file);
} catch (IOException e) {
e.printStackTrace();
}
The following code supposed to open Folder2, but instead it opens D:\Folder\Folder2.bat file.
How to fix that?
Opening folder through Deskopt.open() would be delegated to Desktop.browseFileDirectory() (JDK 8/9)
But, as seen in JDK-8233994, that is not supported/implemented for Windows.
So the alternative with explorer.exe is indeed the recommended way:
Process p = new ProcessBuilder("explorer.exe", "/select,D:\\Folder\\Folder2").start();
The Desktop API describes the method open() clearly:
Launches the associated application to open the file. If the specified
file is a directory, the file manager of the current platform is
launched to open it.
Or see the reply of #vonC
If I run my Java program in NetBeans and follow the information given in the output window to run from a command line:
To run this application from the command line without Ant, try:
java -jar "C:\Users\erdik\OneDrive\Documents\Computing Science Degree\Course Folder\Year 1\Programming 1\Assignment 2 - Year 2 Edit\assignment2\dist\assignment2.jar"
The program starts to run, but when it comes to run the following code to open a .txt file (my "database"):
System.out.println("Loading database of stored transactions...");
try
{
file = new File("TransactionDetails.txt");
inFile = new Scanner(file);
}
// if the log couldn't be found in the default program location
catch (FileNotFoundException ex)
{
System.out.println(CustomMessages.FileNotFound() +
System.getProperty("user.dir")); // display default directory
System.out.println(CustomMessages.systemExit());
System.exit(1); // the program needs the log to function as intended
}
It cannot find the .txt file and prints the default directory as the Windows System32 folder. How can I specify the location to be the Project folder as expected?
You could use an absolute path to the file instead of a relative path. e.g
file = new File("C:\Users\erdik\OneDrive\Documents\Computing Science Degree\Course Folder\Year 1\Programming 1\Assignment 2 - Year 2 Edit\assignment2\dist\TransactionDetails.txt");
inFile = new Scanner(file);
You cannot rely on the current working directory to be set to anything.
Either provide the file as a class path resource instead or ask the jvm where the class is located in the file system and locate the file relative to that.
For a read only file I would consider providing it as a resource.
I have installed wkhtmltopdf utility and its accessible via mac terminal. But when I'm trying to access it via java code, I get the following error
Cannot run program "wkhtmltopdf": error=2, No such file or directory
I am using this wrapper of wkhtmltopdf https://github.com/jhonnymertz/java-wkhtmltopdf-wrapper
Same code is perfectly running fine in windows system. So i believe issue is something related to tomcat not able to access the wkhtmltopdf utility.
Here is the code that i am using,
Pdf pdf = new Pdf();
pdf.addPage(serverBasePath + "/htmlview", PageType.url);
// Save the PDF
pdf.saveAs(filePath + "\\" + filename);
You need to tell java-wkhtmltopdf-wrapper were the actual program is on the disk. Try this:
WrapperConfig wc = new WrapperConfig("C:\\Program Files\\wkhtmltopdf\\wkhtmltopdf.exe");
Pdf pdf = new Pdf(wc);
...
pdf.saveAs(...);
I have been breaking my head for two days trying to fix the file permissions for my tomcat7 server. I have a library class (.jar file included in myapp/WEB-INF) which needs to run a shell script. The library is written by me and works fine within NetBeans ie. no hassle in creating,reading and deleting files. That is because NetBeans runs the program as blumonkey(my username on my Ubuntu System). But when I import this into tomcat and run it, tomcat "executes" the command, produces no definite output, tries to check for a file(which will be generated when the script succeeds) and throws a FileNotFoundException.
More Details as follows:
Tomcat7 installed using apt-get, has its data in 2 locations - /var/lib/tomcat7 with conf and webapps folders and /usr/share/tomcat7 with the bin and lib folders
The user uploads a .zip file which is stores to /home/blumonkey/data. Rest of the program runs on the documents stored here. All new folders/files uploaded by tomcat have, obviously, tomcat7 as the owner.
I have tried things like changing the ownership to blumonkey, adding tomcat7 to blumonkey user group but none of the methods worked (Somewhere around here I probably messed up changing permissions carelessly :/ ). Apparently tomcat7 is unable to process on the files it owns.(How can this be?).
The script works when I run it in the terminal. But it doesn't work when I do a sudo -u tomcat7 script.sh, ie run it as tomcat7. It just exits with no message. I doubt that this it what is happening as I have tried to debug by redirecting the errors and outputs in ProcessBuilder but they came empty.
Any help regarding how to fix the issue and get the script running would be greatly appreciated. Please comment if you need any more info.
The code for script execution
private static void RunShellCommandFromJava(String command,String fn, String arg1,String arg2) throws Exception
{
try
{
System.out.println(System.getProperty("user.name"));
ProcessBuilder pbuilder = new ProcessBuilder("/bin/bash",command,fn,arg1,arg2);
System.out.println(pbuilder.command());
pbuilder.redirectErrorStream(true);
Process p = pbuilder.start();
p.waitFor();
}
catch(Exception ie)
{
throw ie;
}
}
The command which needs to be executed
"/bin/bash /abs/path/to/script.sh /abs/path/to/doc/in/data-folder maxpages=30 maxsearches=3"
PS : I have followed this question but it didn't help. I also tried other options like Runtime.exec(), bash,/bin/bash/ and /bin/bash/ -c, some of them don't work at all, others give no results.
Try to use Runtime and check standard error to find out what was the problem (probably permissions or paths):
// run command
String[] fixCmd = new String[] { "/bin/bash", "/abs/path/to/script.sh", "/abs/path/to/doc/in/data-folder", "maxpages=30", "maxsearches=3" };
Process start = Runtime.getRuntime().exec(fixCmd);
// monitor standard error to find out what's wrong
BufferedReader r = new BufferedReader(new InputStreamReader(start.getErrorStream()));
String line = null;
while ((line = r.readLine()) != null) {
System.out.println(line);
}
I tried the code below to run a stand-alone utility app I created from Apple script but, I get a No File or Directory Exists error.
I put identical copies (for testing) in the project, dist, parent directories but, it didn't help.
So, my questions are:
Is my call to run the app bad (perhaps because it's not a Windows exe)?
How to run a mac app from java?
Thanks
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
Runtime r=Runtime.getRuntime();
Process p=null;
String s="MyLineInInput.app";
try {
p = r.exec(s);
} catch (IOException ex) {
Logger.getLogger(AudioSwitcherView.class.getName()).log(Level.SEVERE, null, ex);
}
}
A Mac App Bunde is not an executable file, it's a folder with a special structure. It can be opened using the open command, passing the App Bundle path as an argument: open MyLineInInput.app.
EDIT:
Even better would be using Desktop.getDesktop().open(new File("MyLineInInput.app"));
I used the Runtime.getRuntime().exec() method with the open command mentioned in the selected answer. I didn't use Desktop.getDesktop().open() since it unwantedly opened a terminal in my case and I didn't want to create an extra File object.
Process process = Runtime.getRuntime().exec("open /System/Applications/Books.app");
Reason for adding '/System':
It seems we need to use the /System prefix for System apps. For user-installed apps, that's not required, and it can be like /Applications/Appium.app.
To answer #Pantelis Sopasakis' issue that I also faced initially -
I get the error message: java.lang.IllegalArgumentException: The file: >/Applications/Microsoft Office 2011/Microsoft\ Excel.app doesn't exist.
In this case, it could be simply due to not escaping the space characters in the path.
Environment: JDK 11 Zulu - macOS Monterey 12.2.1 - M1 Silicon