This question already has answers here:
Open a text file in the default text editor... via Java?
(3 answers)
Closed 4 years ago.
I want to open a txt file using java
For running .exe I use this:
try {
Runtime.getRuntime().exec("c:\\windows\\notepad.exe");
} catch (Exception e) {
e.printStackTrace();
}
I have tried to run .txt file and it doesn't work. I get IOException with this message:
CreateProcess error=193, %1 is not a valid Win32 application
How I can run a .txt using java?
You cannot "run" a .txt file. Because a text file simply respresents a set of characters with a certain encoding. Whereas on the other hand an exe is a file containing compiled code. That is information specifically for the machine to understand.
If, like in your example above, you want to open a textfile in Notepad, you have a few options. One goes as follows
try {
Runtime.getRuntime().exec(new String[] { "c:\\windows\\notepad.exe", "C:\\path\\to\\the.txt" });
} catch (Exception e) {
e.printStackTrace();
}
Notepad is already set in your PATH environment variable, you miss only the paremeter: the file to be opened:
Runtime.getRuntime().exec("start notepad 'PATH/TO/file.txt'");
FYI notepad argument list:
/A <filename> open file as ansi
/W <filename> open file as unicode
/P <filename> print filename
/PT <filename> <printername> <driverdll> <port> print filename to designated printer
Related
i have written a python script to goto a website and scrape some text off the website and save that text into a text file on my computer
from selenium import webdriver
import os
chrome_path = r"C:\tf_alert\chromedriver.exe"
driver = webdriver.Chrome(chrome_path)
driver.get("https://www.minuteinbox.com/")
email = driver.find_element_by_xpath("""/html/body/div[2]/div[3]/div[1]/div[3]/div/span""").text
strEmail = str(email)
mailMan = open("10MAIL.txt", "a")
mailMan.write(strEmail)
mailMan.close()
os.system("taskkill /im py.exe")
when i run the script from where it is located on my computer all works fine and text is properly written to text file on my computer
but when i try to integrate the python script into a java program (code below), the text scraped off of the website does not get written to text file
public void SimpleTest() throws InterruptedException, IOException {
Desktop desktop01 = Desktop.getDesktop();
File file01 = new File("C:\\tf_alert\\other python projects\\mailMan.py");
if (file01.exists()) {
desktop01.open(file01);
}
Thread.sleep(20000);
StringBuilder contentBuilder01 = new StringBuilder();
try (Stream<String> stream = Files.lines(Paths.get("C:\\tf_alert\\other python projects\\10MAIL.txt"), StandardCharsets.UTF_8)) {
stream.forEach(s -> contentBuilder01.append(s).append(""));
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(contentBuilder01);
PrintWriter pw = new PrintWriter("C:\\tf_alert\\other python projects\\10MAIL.txt");
pw.close();
}
the purpose of the java program above is to activate the python script to scrape text off a website, then get the text written from the python script from the file on my computer, print the text to console, and then clear the text file so that all is ready for next execution
the problem is occuring during the writing of text mailMan.write(strEmail) in python code because when java prints the text from the text file it prints a blank ""
i suppose there is interference between java and python
can anyone help out?
You are not executing the python script.
Running a .py file from Java should help you.
Another source: Three ways to run Python programs from Java
to fix this problem i created a windows batch file that changes directory to where the python file is located and starts that file
cd C:\your-directory
start yourscript.py
I'm trying to decompress a rar file, using the runtime but it doesn't works!, just open a prompt saying that can't find the file
this is the code for it:
try {
Runtime.getRuntime().exec("C:\\Program Files (x86)\\WinRAR\\WinRAR.exe X *ok*.rar F:\\");
} catch (IOException ex) {
System.out.println(ex);
}
also i've used the processbuilder and that's worse, dosen't do anything ¬_¬
ProcessBuilder b = new ProcessBuilder("C:\\\\Program Files (x86)\\\\WinRAR\\\\WinRAR.exe X *sok*.rar F:\\");
here is where i find the information about the winrar
looks like path issue.( C:\Program Files (x86)\WinRAR\WinRAR.exe X ok.rar F:\")
"\" may be used along with roots eg(c:\, D:\) and then follows "\"
D:\programfiles\winar
Using Java program I need to run/open/edit any file. This should have similar effect of double clicking file in File Explorer and OS will execute file if it an executable OR open/edit it in it's respective registered program.
I have tried the Runtime.exec() method (See down there) but that method only runs executable files. I need mine to run any file. This includes text files, audio files, pictures, anything.
I have tried the following:
Runtime.getRuntime().exec("README.txt");
Have you consider trying to use the java.awt.Desktop class?
For example...
if (Desktop.isDesktopSupported()) {
try {
if (Desktop.getDesktop().isSupported(Desktop.Action.EDIT)) {
Desktop.getDesktop().edit(new File("Readme.txt"));
}
// or...
if (Desktop.getDesktop().isSupported(Desktop.Action.OPEN)) {
Desktop.getDesktop().open(new File("Readme.txt"));
}
} catch (IOException exp) {
exp.printStackTrace();
}
}
This will attempt to open/edit the file in the OS specified editor for the given file
This question already has answers here:
How do I run a batch file from my Java Application?
(12 answers)
Closed 8 years ago.
Lets suppose I have a listener for a Button
public class Visualizer1 implements ActionListener {
public void actionPerformed(ActionEvent a) {
try {
Runtime rt2 = Runtime.getRuntime();
Process p = rt2.exec("visualizer/vis1.exe");
InputStream in = p.getInputStream();
OutputStream out = p.getOutputStream();
InputStream err = p.getErrorStream();
p.destroy();
} catch (Exception exc) {/* handle exception */
}
the "vis1.exe" will execute without any problem and it will open up
but if I have an application with a ".bat" extension like if it was(vis1.bat), it won't open up.
Note: .bat extension is an executable file
Try this..
Runtime.getRuntime().exec("cmd /c start vis1.bat");
a .bat isnt an executable file.
"A .BAT (short for "batch") file is a plain text file that contains a series of Windows commands. An .EXE (short for "executable") file is a binary file that contains much more complex executable binary code."
http://www.fileinfo.com/help/bat_vs_exe_files
Have you gone through previous threads on same issue on stackoverflow.com?
Have a look at followings:
How do I run a batch file from my Java Application?
Run batch file from Java code
How to execute a batch file from java?
Run a batch file with java program
I have a table of data and i wanted an export function, this application run on web so i want to create a csv file and then offer (open or save as option) usual download options. Im using CSV writer at the moment.
The line in question here is Runtime.getRuntime().exec("export.csv"); give error listed at bottom
How would i do this?
Here is the action tied to a button
Action exportData = new Action() {
private static final long serialVersionUID = -7803023178172634837L;
#Override
public void execute(UIContext uic, ActionEvent event) {
try{
CSVWriter writer = new CSVWriter(new FileWriter("export.csv"), '\t');
int i = 0;
while (i < forExport.getTotalCount()){
String[] entries = {
forExport.getSearchResults().get(i).getName().getGivenNames() + forExport.getSearchResults().get(i).getName().getSurname(),
forExport.getSearchResults().get(i).getId()
};
writer.writeNext(entries);
i++;
}
writer.close();
Runtime.getRuntime().exec("export.csv");
}catch(Exception e){
system.out.print(e);
}
}
};
getting error java.io.IOException: Cannot run program "export.csv": CreateProcess error=193, %1 is not a valid Win32 applicationDEBUG
NOTE: i do not want it to automatically open in excel, i want the option to save or open.
I don't understand why you are exec-ing anything whatsoever. This is server code. You don't want to exec Excel at the server at all. You want to write the file back to the browser, along with a content-disposition header.
You should try
Runtime.getRuntime().exec("excel.exe export.csv");
export.csv is not a valid windows command.
A .csv file is a comma separated value file which is used to keep data. and the parameter of the exec command is the command as string to open the .csv file. But in your command string you have not specified any program with which the runtime should try to open the .csv file.
As a .csv file is not executable, In the command string parameter specify the program name with ahich you want to open the ..csv file.