When I run my AutoIt script for a filename without spaces ("filename.txt") it gets executed successfully. But when filename contains spaces ("File Name.txt") I get error "File not found".
Parameterized.au3 :
ControlFocus("Open","","Edit1")
ControlSetText("Open","","Edit1",$CmdLine[1])
ControlClick("Open","","Button1")
Execution from Java:
Runtime.getRuntime().exec("C:\\Users\Screenshots\\Parameterized.exe" + " "
+ filePath);
filePath is passed as argument from another method :
filePath-> "C:\\Temp\\TMP\\TCs\\TC1\\Solution File.txt"
Take your file path into " so that your exec would look like:
Runtime.getRuntime().exec("C:\\Users\Screenshots\\Parameterized.exe" + " "
+ "\"" + filePath + "\"");
Related
I have a CSV file, and I'm trying to import the CSV file in the MongoDB database. Unfortunately, the experience is not working as I wish it would. The content of the CSV file is not relevant, since when I use MongoDB Compass to import it manually, there is no issue and it's appearing as I want. Thus, I'm trying to save it from local folder to the MongoDB using Java and Spring Boot if possible, but I can't find any good advice or explaination about how to do so.
I'm using Spring Boot 2.6.3 and Java11.
Thanks a lot.
public class MongoImportUtil {
public static void main(String[] args) {
String Host = "localhost";
String Port = "27017";
String DB = "userDemo";
String CollectionName = "users";
String FileName = "D:/Test.csv";
// Without Credential
String command = "C:\\Program Files\\MongoDB\\Server\\5.0\\bin\\mongoimport.exe --host " + Host + " --port " + Port + " --db " + DB + " --collection " + CollectionName + " --headerline --type=csv --file "+ FileName;
// With Credential
/* String command = "C:\\Program Files\\MongoDB\\Server\\5.0\\bin\\mongoimport.exe --host " + Host + " --port " + Port + " --db " + DB + " --collection " + CollectionName + "--authenticationDatabase admin --username " + user + " --password " + password + " --headerline --type=csv --file "+ FileName; */
try {
Process process = Runtime.getRuntime().exec(command);
System.out.println("Imported csv into Database");
} catch (Exception e) {
System.out.println("Error executing " + command + e.toString());
}
}
}
I'm trying to create an installer for the java program it's supposed to download a zip file from a URL. to do this I'm using the Wget command to download the files in a specific directory. I made it so the code prints out the command it's executing so I can copy and paste it to test. When I test the command it works perfectly, but when I run the code to do it automatically it corrupts the zip file. it won't unzip, and if i transfer the file to my mac it says Unable to expand filename.zip it is an unsupported file I believe this is because the file is corrupted.
This is the command it runs
wget -O /home/pi/1.2.1.zip https://install.swiftpcb.tech/1.2.1.zip
unzip /home/pi/1.2.1.zip -d /home/pi
this is the output of running the unzip one manually
Archive: 1.2.1.zip
End-of-central-directory signature not found. Either this file is not
a zip file, or it constitutes one disk of a multi-part archive. In the
latter case, the central directory and zip file comment will be found on
the last disk(s) of this archive.
unzip: cannot find zipfile directory in one of 1.2.1.zip or
1.2.1.zip.zip, and cannot find 1.2.1.zip.ZIP, period.
this is the class running the command in java (you only need to focus what in the if statement ran == 0
package Installer;
import java.io.IOException;
import java.sql.*;
public class Main {
public void start() throws IOException, InterruptedException {
// SQLite connection string
String url = new Info().getConfigDir();
// SQL statement for creating a new table
String sql = "CREATE TABLE IF NOT EXISTS appInfo (\n"
+ " \"ID\" INTEGER,\n"
+ " \"version\" TEXT,\n"
+ " \"ran\" INTEGER DEFAULT 0,\n"
+ " PRIMARY KEY(\"ID\" AUTOINCREMENT) \n"
+ ");";
try (Connection conn = DriverManager.getConnection(url);
Statement stmt = conn.createStatement()) {
stmt.execute(sql);
} catch (SQLException e) {
System.out.println(e.getMessage());
}
SQLite lite = new SQLite();
lite.createData();
int versionChangeStatus = lite.updateVersionInDB();
if(versionChangeStatus != 0) System.out.println("Something went wrong");
int ran = lite.getRanValue();
if(ran == 0){
String version = new Info().getVersion();
//for deploy
Process p = Runtime.getRuntime().exec("wget -O /home/pi/" + version + ".zip " + "https://install.swiftpcb.tech/" + version + ".zip" + " && unzip /home/pi/" + version + ".zip -d /home/pi");
System.out.println("wget -O /home/pi/" + version + ".zip " + "https://install.swiftpcb.tech/" + version + ".zip" + " | unzip /home/pi/" + version + ".zip -d /home/pi");
Thread.sleep(2 * 1000);
p = Runtime.getRuntime().exec("unzip /home/pi/1.2.1.zip -d /home/pi");
// Process p = Runtime.getRuntime().exec("wget https://install.swiftpcb.tech/" + version + ".zip" + " && unzip" + version + ".zip -d /home/pi ");
int updateStat = lite.updateRanValue("1");
if(updateStat != 0) System.out.println("Something went wrong");
}
}
}
Goodmorning everyone.
I have a problem with my java program: i would like to create a directory to store a .sql file, which will be generated through mysqldump.exe. I've been trying to follow guides on Stack Overflow or others, but i still can't resolve my problem: directory and .sql file are generated, but the file is totally blank. The best part is that pasting the same command (ctrl+C, ctrl+V) on the command line, it works well and generates an ordinary .sql file for a dump operation.
Please help me. I'm using NetBeans 8.0.2 as IDE, there's my code (there are some variables, like SelectedTable, that allows me to get the current user, password etc. Don't care about them: those parts works and the command is correctly generated):
private void jMenuExternalBackupActionPerformed(java.awt.event.ActionEvent evt) {
LocalDate today = LocalDate.now();
GregorianCalendar now = new GregorianCalendar();
String BackupFolderName = SelectedTable.getMaster().getName()+" "+SelectedTable.getName()+" "+today.getYear()+"_"+today.getMonth().getValue()+"_"+today.getDayOfMonth()+" "+now.get(now.HOUR_OF_DAY)+"_"+now.get(now.MINUTE)+"_"+now.get(now.SECOND);
new File("/HyperSQL/Backups/").mkdirs();
new File("/HyperSQL/Backups/"+BackupFolderName+"/").mkdirs();
String command;
if (SelectedTable.getMasterApplication().getPassword().isEmpty())
command = "mysqldump -u" + SelectedTable.getMasterApplication().getUserLogged() + " " + SelectedTable.getMaster().getName() + ""
+ " > \"C:\\HyperSQL\\Backups\\"+BackupFolderName+"\\backup.sql\"";
else
command = "mysqldump -u" + SelectedTable.getMasterApplication().getUserLogged() + " -p" + SelectedTable.getMasterApplication().getPassword() + " " + SelectedTable.getMaster().getName() + ""
+ " > \"C:\\HyperSQL\\Backups\\"+BackupFolderName+"\\backup.sql\"";
try
{
Process exec = Runtime.getRuntime().exec(new String[]{"cmd.exe","/c", command});
}
catch (Exception e)
{
e.printStackTrace();
}
}
I have a short question concerning the Runtime.getRuntime.exec("") command in java.
I am trying to make a tunnel to a gateway computer via SSH:
String tunnel = "C:\\Program Files\\PuTTY\\putty.exe -ssh -X -P " + localPort + " " + tempUsername + "#" + localIP
+ " -pw " + tempPassword + " -L " + tunnelPort + ":" + gatewayName + ":"+gatewayPort;
Runtime.getRuntime().exec(tunnel);
This bit of code works properly except the annoying fact that a command prompt appears.
Now I tried to exit the prompt after executing the code:
String tunnel = "C:\\Program Files\\PuTTY\\putty.exe -ssh -X -P " + localPort + " " + tempUsername + "#" + localIP
+ " -pw " + tempPassword + " -L " + tunnelPort + ":" + gatewayName + ":"+gatewayPort;
String cmdCommands [] = {tunnel, "exit"};
Runtime.getRuntime().exec(cmdCommands);
Is it possible to close the command prompt in a similar way like I do or are there better ways? (This code doesnt work)
You'll need to either use an actual SSH library directly instead of putty, as in the comments, or capture the IO streams of the exec
Process p = Runtime.getRuntime().exec(cmdCommands);
InputStream is = p.getInputStream();
OutputStream os = p.getOutputStream();
os.write("exit\n");
It's generally a bad idea to hard code \n, for platform reasons, but you get the idea. Also, you will need to pick the right OutputStream. There are several subclasses(buffered etc.) which may be useful.
Method below has function to simply move files from the "working" to the "move" directory which paths it receives through the method call. It all works, but for the case when file name has name with two extensions (like .xml.md5) where the .renameTo method returns false. Is there a way to alter the below code so it would work regardless of the OS that it's run on. (Currently it is the Windows)
public void moveToDir(String workDir, String moveDir) throws Exception {
File tempFile = new File(workDir);
File[] filesInWorkingDir = tempFile.listFiles();
for (File file : filesInWorkingDir) {
System.out.println(file.getName());
if (new File(moveDir + File.separator + file.getName()).exists())
new File(moveDir + File.separator + file.getName()).delete();
System.out.println(moveDir + File.separator + file.getName());
Boolean renameSuccessful = file.renameTo(new File(moveDir + File.separator + file.getName()));
if (!renameSuccessful) throw new Exception("Can't move file to " + moveDir +": " + file.getPath());
}
}
I have simplified your code and added a check if delete was successful. Try it.
public void moveToDir(String workDir, String moveDir) {
for (File file : new File(workDir).listFiles()) {
System.out.println(file.getName());
final File toFile = new File(moveDir, file.getName());
if (toFile.exists() && !toFile.delete())
throw new RuntimeException("Cannot delete " + toFile);
System.out.println(toFile);
if (!file.renameTo(toFile))
throw new RuntimeException(
"Can't move file to " + moveDir +": " + file.getPath());
}
}