I'm trying to create a program that creates a copy of itself and deletes the original project folder.
(FYI: Project has its own JRE inside it)
(FYI: This program runs on Windows)
So:
To be able to do that, other than my primary main method, there is a second main method in the class called SelfUpdater.
Inside my main thread I copy the project to a second folder.
And then run the SelfUpdater's main method inside this second project folder:
This should mean that I'm running a whole new instance, totally unrelated to the first java.exe (which already gets closed with system.exit(0) as soon as new instance starts).
But when I try to delete the first folder, I get the error "Error deleting old client.java.io.IOException: Unable to delete file". Actually It deletes some of the files, but I can't delete Application.exe and its lib folder.
Its folder is not open in windows. It is not being used by anything else. I can't delete the file manually either (Windows says it is in use). And as soon as the second java.exe is terminated, I can delete it.
I can't give a total working example, But my in my main thread I call this following method:
public static void selfUpdate() {
try {
String separator = System.getProperty("file.separator");
String classpath = System.getProperty("java.class.path");
String rootPath = System.getProperty("user.dir");
String path = null;
File parentFolder = null;
File originalClientFolder = new File(rootPath);
parentFolder = originalClientFolder.getParentFile();
File secondClientFolder = new File(parentFolder.getAbsolutePath() + separator + "runLAST");
FileUtils.copyDirectory(originalClientFolder, secondClientFolder);
path = secondClientFolder.getAbsolutePath() + separator + "jre8" + separator + "bin" + separator + "java";
ProcessBuilder processBuilder = new ProcessBuilder(path, "-cp", classpath, SelfUpdater.class.getName(), downloadURL, rootPath);
processBuilder.directory(secondClientFolder);
processBuilder.start();
System.exit(0);
} catch (Exception ex) {
}
}
And my SelfUpdater class' main is:
public static void main(String[] args) {
try {
String originalClientFolderPath = args[1];
//
File oldClientFolder = new File(originalClientFolderPath);
System.out.println("Deleting old client recursively. Folder: " + oldClientFolder.getAbsolutePath());
try {
FileUtils.deleteDirectory(oldClientFolder);
} catch (Exception ex) {
}
}
}
Appearently, I forgot to change the classpath.
I'm thinking about leaving the question, since people may need such code part.
But at the end, method that's being called is changed to this:
public static void selfUpdate() {
try {
String separator = System.getProperty("file.separator");
String classpath = System.getProperty("java.class.path");
String rootPath = System.getProperty("user.dir");
String path = null;
File parentFolder = null;
File originalClientFolder = new File(rootPath);
parentFolder = originalClientFolder.getParentFile();
File secondClientFolder = new File(parentFolder.getAbsolutePath() + separator + "runLAST");
FileUtils.copyDirectory(originalClientFolder, secondClientFolder);
// ADDED: --------------------------------------------------------
String origialClientFolderName = originalClientFolder.getName();
classpath = classpath.replace(origialClientFolderName, "runLAST");
// ---------------------------------------------------------------
path = secondClientFolder.getAbsolutePath() + separator + "jre8" + separator + "bin" + separator + "java";
ProcessBuilder processBuilder = new ProcessBuilder(path, "-cp", classpath, SelfUpdater.class.getName(), downloadURL, rootPath);
processBuilder.directory(secondClientFolder);
processBuilder.start();
System.exit(0);
} catch (Exception ex) {
}
}
Related
I have the following restart method, which is working:
public static void restart(String[] args) {
ArrayList<String> commands = new ArrayList<String>();
List<String> jvmArgs = ManagementFactory.getRuntimeMXBean().getInputArguments();
// Java
commands.add(System.getProperty("java.home") + File.separator + "bin" + File.separator + "java");
// Jvm arguments
for (String jvmArg : jvmArgs) {
commands.add(jvmArg);
}
// Classpath
commands.add("-cp");
commands.add(ManagementFactory.getRuntimeMXBean().getClassPath());
// Class to be executed
commands.add(BaseUI.class.getName());
// Command line arguments
File workingDir = null; // Null working dir means that the child uses the same working directory
String[] env = null; // Null env means that the child uses the same environment
String[] commandArray = new String[commands.size()];
commandArray = commands.toArray(commandArray);
try {
Runtime.getRuntime().exec(commandArray, env, workingDir);
System.exit(0);
} catch (IOException e) {
e.printStackTrace();
}
}
I want to put the Look And Feel configuration on the main method of my main application but, while restarting, the execution does not enter in that part of the code. So, obviously, the Look and Feel is not changed at all.
Is there any workaround to solve this?
In an application I am writing, I need to use the javaagent option to call an external jar in the same folder as my current jar file. When I run the code from the jar file, I get told: "Error: Could not find or load main class -javaagent:" but when I am running it from a batch file, it works as excepted.
I am using a ProcessBuilder to start the application:
String java = System.getProperty("java.home") + File.separatorChar + "bin" + File.separatorChar +"java.exe";
File transagent = new File(pluginDir + File.separatorChar + "TransAgent.jar");
String doublequote = String.valueOf('"');
List<String> commandlist = new ArrayList<String>();
commandlist.add(java);
commandlist.add(" -javaagent:");
commandlist.add(doublequote);
commandlist.add(transagent.getAbsolutePath());
commandlist.add(doublequote);
for(int i = 0; i < commandlist.size(); i++){
String part = commandlist.get(i);
System.out.print(part);
}
System.out.println();
ProcessBuilder pb = new ProcessBuilder();
pb.command(commandlist);
pb.redirectError(Redirect.appendTo(errorfile));
pb.redirectOutput(Redirect.appendTo(logfile));
try {
pb.start();
} catch (IOException e) {
e.printStackTrace();
}
But, when I go to the error file, I see "Error: Could not find or load main class -javaagent:"
This would usually be thrown if the option isn't valid, but I've checked the dash to work file. And I put what printed from the application in a batch file, and it worked fine. Why?
You can try this code below:
ProcessBuilder pb = new ProcessBuilder("java", "-javaagent:"+transagent.getAbsolutePath(), "YouMainClass");
pb.redirectError(Redirect.appendTo(errorfile));
pb.redirectOutput(Redirect.appendTo(logfile));
try
{
pb.start();
}
catch(IOException e)
{
e.printStackTrace();
}
Hi Ive made a Java program which read lines from a CSV as data records. The problem im getting is the program works perfectly in eclipse console, however when I try to run the problem from cmd using Java compiler I doesnt work where my code specifies the location of the CSV file its show a error there as unknown. Ive tried everything such as moving the file into another package or using some other code to read the file Im getting the same result which is in eclipse it works but in the actual cmd it not working. Bellow is the ERROR and the method which reads the file.
java.io.FileNotFoundException: src\PostCodeENW.csv (The system cannot find the path specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at Java.io.FileReader.<init>(Unknown Source)
public void loadfile() {
System.out.println(System.getProperty("user.dir"));
String csvFile = "src/PostcodeENW.csv";
BufferedReader br = null;
String line = "";
try {
br = new BufferedReader(new FileReader(csvFile));
while ((line = br.readLine()) != null) {
String[] ar = line.split("/");
PostCode = ar[0].trim().replaceAll(" ", " ");
String[] ar1 = PostCode.split("\\s");
String PostCodeP1 = ar1[0];
String PostCodeP2 = ar1[1];
int Easting = Integer.parseInt(ar[1]);
int Northing = Integer.parseInt(ar[2]);
String Ward = ar[3];
Location geo = new Location(PostCodeP1, PostCodeP2, Easting, Northing, Ward);
map.put(geo.getkey(), geo.getValue());
// System.out.println(PostCodeP1 + " " + PostCodeP2 + " " + Easting + " " + Northing + " " + Ward);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
System.out.println("Done Adding all records");
System.out.println("Database currently holds: " + map.size()); //Test how much elements are currently in the HashMap
}
Where are you running the java file from? It's looking for a folder "src" in the current directory, then a file "PostcodeENW.csv" in the folder "src".
So if the command line is in a different directory to where that folder and file are located it would give you an error.
Try to run the java file in the same directory as where the folder "src" is located
In command line change directory to the eclipse workspace using cd %HOMEPATH%/Documents/workspace
If the eclipse workspace is in your user's folder, in your documents, in a folder called workspace.
Once compiled, the src path will no longer exist. As I understand eclipse, it will only jar class files from the src folder, this means, once compiled and packaged, it's unlikely that the file PostcodeENW.csv will even exist.
Two things you need to try,
First, move the PostcodeENW.csv file into the resources directory within your project
Once build and a packaged, eclipse will include the contents of the resources directory within the jar. This means that these resources will no longer be accessible as files,but will become embedded resources
Secondly, change your code so that when you read the file, you read it as an embedded resource, for example
String csvFile = "/PostcodeENW.csv";
//...
br = new BufferedReader(new InputStreamReader(getClass().getResourceAsInputStream(csvFile)));
Nb- I don't use eclipse, so I'm going what I understand needs to be done, but these are the basic ideas ;)
am asking this question after doing a lot of research and also implementing it in my code after the research but I ended up with FileNotFoundException.What exactly am doing here is I want to avoid hardcoding in my java code so am creating a properties file with name as Constants.properties and am calling it in my java code. but it says that it is not finding the file. My properties file is in the src folder of the project. Below is the code snippet. Any suggestions?
Properties file:
executable.run = C:\\server\\lrd.exe
incoming.file = C:\\file\\test.ic
executable.params1 = -z
executable.params2 = -a[+]
log.file = C:\\TESTFile\\test.txt
Java Code: This is the class file which has the properties file details.
public class PropInfo {
static private PropInfo _instance = null;
public String executable = null;
public String filein = null;
public String params1 = null;
public String params2 = null;
public String log = null;
protected PropInfo(){
try{
InputStream file = new FileInputStream(new File("Constants.properties"));
Properties props = new Properties();
props.load(file);
executable = props.getProperty("executable.run");
filein = props.getProperty("incomin.file");
params1 = props.getProperty("executable.params1");
params2 = props.getProperty("executable.params2");
log = props.getProperty("log.file");
}
catch(Exception e){
System.out.println("error" + e);
}
}
static public PropInfo instance(){
if(_instance == null){
_instance = new PropInfo();
}
return _instance;
}
}
Main Class:
try{
PropInfo propinfo = PropInfo.instance();
String connString = propinfo.executable + " " + propinfo.params1 + " " +
propinfo.filein + " " + propinfo.params2 + " " + " " + propinfo.log ;
Runtime rt = Runtime.getRuntime();
// Process pr = rt.exec
// (PropInfo.executable+" "+PropInfo.params1+" "+PropInfo.filein+" "
//+PropInfo.params2+" "+PropInfo.log);
Process pr = rt.exec(connString);
BufferedReader input = new BufferedReader(new InputStreamReader (pr.getInputStream()));
String line=null;
StringBuffer start= new StringBuffer();
while((line=input.readLine()) != null) {
start.append("Started" + line + "\n");
System.out.println(line);
}
// System.out.println("browse");
}
catch (Throwable t)
{
t.printStackTrace();
}
finally
{
}
Gives this Exception:
errorjava.io.FileNotFoundException: Constants.properties (The system cannot find the
file specified)
java.io.IOException: Cannot run program "null": CreateProcess error=2, The system
cannot find the file specified
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1042)
at java.lang.Runtime.exec(Runtime.java:615)
at java.lang.Runtime.exec(Runtime.java:448)
at java.lang.Runtime.exec(Runtime.java:345)
at com.emc.clp.license.StartTest.main(StartTest.java:44)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the
file specified
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(ProcessImpl.java:288)
at java.lang.ProcessImpl.start(ProcessImpl.java:133)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1023)
... 4 more
Yes, don't put your properties file into the src folder. Put it where you start the jvm from (or provide an absolute path). Also I really suggest getting rid of forward slashes in path names.
UPDATE: Add this to find out where to put your file:
System.out.println(new File(".").getAbsolutePath());
The way you load Constants.properties it should be right under your src package package at the level where your packaging starts.
for example,
if you hava src/java/propinfopackage/PropInfo
put it inside java folder and call it as follows
InputStream propertiesInputStream = null;
Properties properties = new Properties();
propertiesInputStream = PropInfo.class.getClassLoader().getResourceAsStream("/Constants.properties");
properties.load(propertiesInputStream);
String value = properties.getProperty("executable.run");
.......
I had the same problem and it was solved like this:
Properties prop = new Properties();
try {
prop.load(getClass().getResourceAsStream("/com/my/package/Constants.properties"));//here your src folder
System.out.println(prop.getProperty("executable.run"));
} catch(IOException e) {}
Make sure your properties file is in root path of the project. Right click on project and paste the properties file. Your error will go.
Also refer #Axel Answer above. It will resolve your issue.
I am trying to create a directory but it seems to fail every time? I have checked that it is not a permission issue too, I have full permission to write to that directory. Thanks in advance.
Here is the code:
private void writeTextFile(String v){
try{
String yearString = convertInteger(yearInt);
String monthString = convertInteger(month);
String fileName = refernce.getText();
File fileDir = new File("C:\\Program Files\\Sure Important\\Report Cards\\" + yearString + "\\" + monthString);
File filePath = new File(fileDir + "\\"+ fileName + ".txt");
writeDir(fileDir);
// Create file
FileWriter fstream = new FileWriter(filePath);
try (BufferedWriter out = new BufferedWriter(fstream)) {
out.write(v);
}
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
private void writeDir(File f){
try{
if(f.mkdir()) {
System.out.println("Directory Created");
} else {
System.out.println("Directory is not created");
}
} catch(Exception e){
e.printStackTrace();
}
}
public static String convertInteger(int i) {
return Integer.toString(i);
}
Calendar cal = new GregorianCalendar();
public int month = cal.get(Calendar.MONTH);
public int yearInt = cal.get(Calendar.YEAR);
Here is the output:
Directory is not created
Error: C:\Program Files\Sure Important\Report Cards\2012\7\4532.txt (The system cannot find the path specified)
It's possibly because File.mkdir creates the directory only if the parent directory exists.
Try using File.mkdirs which creates all the necessary directories.
Here's the code which worked for me.
private void writeDir(File f){
try{
if(f.mkdirs()) {
System.out.println("Directory Created");
} else {
System.out.println("Directory is not created");
}
} catch(Exception e){
// Demo purposes only. Do NOT do this in real code. EVER.
// It squashes exceptions ...
e.printStackTrace();
}
}
The only change I made was to change f.mkdir() to f.mkdirs() and it worked
I think that #La bla bla has nailed it, but just for completeness, here are all of the things that I can think of that could cause a call to File.mkdir() to fail:
A syntax error in the pathname; e.g. an illegal character in a file name component
The directory to contain the final directory component does not exist.
There is already something with that name.
You don't have permission to create a directory in the parent directory
You don't have permission to do a lookup in some directory on the path
The directory to be created is on a read-only file system.
The file system gave a hardware error or network related error.
(Obviously, some of these possibilities can be quickly eliminated in the context of this question ...)