I have a simple updater for my application. In code i am downloading a new version, deleting old version and renaming new version to old.
It works fine on Linux. But doesn't work on Windows. There are no excepions or something else.
p.s. RemotePlayer.jar it is currently runned application.
UPDATED:
Doesn't work - it means that after file.delete() and file.renameTo(...) file still alive.
I use sun java 7. (because I use JavaFX).
p.s. Sorry for my English.
public void checkUpdate(){
new Thread(new Runnable() {
#Override
public void run() {
System.err.println("Start of checking for update.");
StringBuilder url = new StringBuilder();
url.append(NetworkManager.SERVER_URL).append("/torock/getlastversionsize");
File curJarFile = null;
File newJarFile = null;
try {
curJarFile = new File(new File(".").getCanonicalPath() + "/Player/RemotePlayer.jar");
newJarFile = new File(new File(".").getCanonicalPath() + "/Player/RemotePlayerTemp.jar");
if (newJarFile.exists()){
newJarFile.delete();
}
} catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
System.err.println("Cannot find curr Jar file");
return;
}
if (curJarFile.exists()){
setAccesToFile(curJarFile);
try {
String resp = NetworkManager.makeGetRequest(url.toString());
JSONObject jsresp = new JSONObject(resp);
if (jsresp.getString("st").equals("ok")){
if (jsresp.getInt("size") != curJarFile.length()){
System.out.println("New version available, downloading started.");
StringBuilder downloadURL = new StringBuilder();
downloadURL.append(NetworkManager.SERVER_URL).append("/torock/getlatestversion");
if (NetworkManager.downLoadFile(downloadURL.toString(), newJarFile)){
if (jsresp.getString("md5").equals(Tools.md5File(newJarFile))){
setAccesToFile(newJarFile);
System.err.println("Deleting old version. File = " + curJarFile.getCanonicalPath());
boolean b = false;
if (curJarFile.canWrite() && curJarFile.canRead()){
curJarFile.delete();
}else System.err.println("Cannot delete cur file, doesn't have permission");
System.err.println("Installing new version. new File = " + newJarFile.getCanonicalPath());
if (curJarFile.canWrite() && curJarFile.canRead()){
newJarFile.renameTo(curJarFile);
b = true;
}else System.err.println("Cannot rename new file, doesn't have permission");
System.err.println("last version has been installed. new File = " + newJarFile.getCanonicalPath());
if (b){
Platform.runLater(new Runnable() {
#Override
public void run() {
JOptionPane.showMessageDialog(null, String.format("Внимание, %s", "Установлена новая версия, перезапустите приложение" + "", "Внимание", JOptionPane.ERROR_MESSAGE));
}
});
}
}else System.err.println("Downloading file failed, md5 doesn't match.");
}
} else System.err.println("You use latest version of application");
}
}catch (Exception e){
e.printStackTrace();
System.err.println("Cannot check new version.");
}
}else {
System.err.println("Current jar file not found");
}
}
}).start();
}
private void setAccesToFile(File f){
f.setReadable(true, false);
f.setExecutable(true, false);
f.setWritable(true, false);
}
I found the solution to this problem. The problem of deletion occurred in my case because-:
File f1=new File("temp.txt");
RandomAccessFile raf=new RandomAccessFile(f1,"rw");
f1.delete();//The file will not get deleted because raf is open on the file to be deleted
But if I close RandomAccessFile before calling delete then I am able to delete the file.
File f1=new File("temp.txt");
RandomAccessFile raf=new RandomAccessFile(f1,"rw");
raf.close();
f1.delete();//Now the file will get deleted
So we must check before calling delete weather any object such as FileInputStream, RandomAccessFile is open on that file or not. If yes then we must close that object before calling delete on that file.
windows locks files that are currently in use. you cannot delete them. on windows, you cannot delete a jar file which your application is currently using.
Since you are using Java 7, try java.nio.file.Files.delete(file.toPath()), it'll throw exception if deletion fails.
There are several reasons:
Whether you have permissions to edit the file in windows.
The file is in use or not.
The path is right or not.
I don't know wich version of Java you are using.
I know when Java was sun property they publish that the Object File can't delete files correctly on windows plateform (sorry I don't find the reference no more).
The tricks you can do is to test the plateform directly. When you are on linux just use the classic File object.
On windows launch a command system to ask windows to delete the file you want.
Runtime.getRuntime().exec(String command);
I just want to make one comment. I learned that you can delete files in Java from eclipse if you run eclipse program as Administrator. I.e. when you right click on the IDE Icon (Eclipse or any other IDE) and select Run as Administrator, Windows lets you delete the file.
I hope this helps. It helped me.
Cordially,
Fernando
Related
I am trying to transfer the data from old textfile to new textfile. Although the code below is able to transfer successfully, it does not delete the old textfile. May I know why is this so?
private void dataTransfer(String oldFilePath, String newFilePath) {
byte[] buffer = new byte[10000];
try {
FileInputStream fileInput = new FileInputStream(oldFilePath);
BufferedInputStream bufferedInput = new BufferedInputStream(fileInput);
FileOutputStream fileOutput = new FileOutputStream(newFilePath);
BufferedOutputStream bufferedOutput = new BufferedOutputStream(fileOutput);
while(true) {
int length = fileInput.read(buffer);
if(length == -1) {
break;
} else {
bufferedOutput.write(buffer);
bufferedOutput.flush();
}
}
fileInput.close();
bufferedInput.close();
fileOutput.close();
bufferedOutput.close();
File oldFile = new File(oldFilePath);
oldFile.delete();
} catch (IOException e) {
e.printStackTrace();
System.out.println(ERROR_TRANSFER_DATA);
}
}
Update the JRE and JDK, make sure you have the rights on the file. Try with a file created by you.
Also, add a catch block for SecurityException
For deleting a file it should work fine but for deleting a directory you have to make sure that Directory is Empty.
You can use the following code block. It works, though don't know. Even without setWritable, it works,
oldFile.setWritable(true);
if(!oldFile.delete()){
System.out.println("de;eted");
}
According to Oracle's docs, the delete method does not guarantee that it will delete the file.
http://docs.oracle.com/javase/7/docs/api/java/io/File.html#delete()
Deleting a file will fail if:
file does not exist
there is a lock on that file it might be opened by another process
file does not exist on the disk
you don't have enough permissions to delete that file (in this case a SecurityException is thrown)
I agree with #panagdu that you might not have sufficient rights to delete the file.
Just as a fluke try closing bufferedStream before fileInputStream
like
bufferedInput.close();
fileInput.close();
bufferedOutput.close();
fileOutput.close();
But I don't think this will help.
Test your code for files with sufficient permission. For example Java does not allow the delete() for system files.
I'm trying to create a Windows installer out of a jar file. Everything has been successful till the final stages.
I used launch4j to wrap the jar file into an exe file then used both, Advanced-Installer and Inno-Setup to create MSI folders. They both work, however, on some computers the exe file that is extracted does not close and can only be killed by using the Task Manager.
In my Java file, I handle the exit process (finally using System.exit(0)) because I would like to ask the user if they wish to save the file before exiting.
This is my code:
exitListener = new ExitListener();
theMainFrame.addWindowListener(new WindowAdapter() {
#Override
public void windowClosing(WindowEvent e) {
ProgramLog.logException(Level.SEVERE, "Problem...WindowsClosing method", new Exception());
exitListener.actionPerformed(null);
}
});
The logger works fine while it's a jar (creates a file and gives an exception), works fine while it's an exe but once I wrap it into an MSI, once opened it does not close and I do not see anything being logged which means it isn't reaching the windowClosing event.
I have tried the exe file by itself on two Windows computers and it works fine (saving and exiting); but once extracted from the installer, it does not quit.
Any suggestions appreciated.
EDIT
So thanks to MadProgrammer I figured out the problem was with the logger itself. Will be editing my code and update depending on how it works out
SOLUTION
So thanks to MadProgrammer, I found out the problem was with the Logger's saving location and not some Windows machine just didn't quit executable files. I have changed the location from the ProgramFiles folder to {user.home}\AppData\Local{Program company}{Program name}
My previous code for the logger was
public ProgramLog() {
try {
FileHandler handler = new FileHandler(logFile);
logger = Logger.getLogger("com.program.msgs");
logger.addHandler(handler);
} catch (Exception e) {
}
}
I have edited it to
public ProgramLog() {
try {
String path = System.getProperty("user.home") + File.separator
+ "AppData" + File.separator + "Local" + File.separator
+ "CompanyName" + File.separator + "CompanyProduct" + File.separator;
File f = new File(path);
f.mkdirs();
FileHandler handler = new FileHandler(path + logFile);
logger = Logger.getLogger("com.program.msgs");
logger.addHandler(handler);
} catch (Exception e) {
}
}
Now my executable works after wrapping it into an MSI!!
My app uses this code to persistently save an ArrayList so it can be recalled when the app is restarted.
private static void storeDevices() {
// Object serialization
try {
FileOutputStream fos = new FileOutputStream("devices.ser");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(devices);
oos.flush();
oos.close();
}
catch(Exception e) {
showAlert("Store Devices", "Exception storing devices to file: " + e);
}
}
Everything works fine when I run from Netbeans. I have wrapped the app in an installer (using Launch4J and Inno Setup Compiler) and when I install the app to C:/Program Files/MyApp (on Windows 7) the above code gives the following exception:
Exception storing devices to file: java.io.FileNotFoundException: devices.ser (Access is denied)
However, when I install to C:/MyApp, everything works fine.
Is this something to do with read/write permission being inherited from the C:/Program Files directory? I know I may be able to solve this be creating the file in C:/temp directory or somewhere like that but I don't want to have to resort to that. So is there any way I can write/read files in my C:/Program Files/MyApp directory?
from windows vista onward, no app can access system folder area without explicit authentication. (folders like program-file/window32/ etc....)
For installed app, authentication is done by windows-installer.
To be able to access (read/write) either you need to run as administrator, or you need to change permission of those folder (which is not recommended)
My solution was to use environment variables to get the correct directory to store application data. Here's the method I created:
private String getAppDataDirectory(String subDirectory, boolean create) {
String appDataDirectory;
try {
appDataDirectory = System.getenv("APPDATA"); //Windows
//todo - could try "ALLUSERSPROFILE"
if (appDataDirectory != null) {
appDataDirectory += File.separator + subDirectory + File.separator;
}
else { //appDataDirectory is null
appDataDirectory = System.getenv("HOME"); //Unix
if (appDataDirectory != null) {
appDataDirectory += File.separator + subDirectory + File.separator;
}
else { //appDataDirectory is still null
throw new Exception("Could not access APPDATA or HOME environment variables");
}
}
}
catch(Exception e) {
e.printStackTrace();
appDataDirectory = "";
}
if (create && appDataDirectory != null && appDataDirectory.length() > 0) {
try {
File dir = new File(appDataDirectory);
dir.mkdir();
}
catch(Exception e) {
e.printStackTrace();
}
}
//log("appDataDirectory: " + appDataDirectory);
return appDataDirectory;
}
I have the following function inside a Stateless EJB running in Glassfish. All it does is write some data to a file. The first part of the function just creates the path to where the file needs to go. The second part actually writes the file.
private boolean createFile(String companyName, String fileName, byte[] data)
{
logger.log(Level.FINEST, "Creating file: {0} for company {1}", new Object[]{fileName, companyName});
File companyFileDir = new File(LOCAL_FILE_DIR, companyName);
if(companyFileDir.exists() == false)
{
boolean createFileDir = companyFileDir.mkdirs();
if(createFileDir == false)
{
logger.log(Level.WARNING, "Could not create directory to place file in");
return false;
}
}
File newFile = new File(companyFileDir, fileName);
try
{
FileOutputStream fileWriter = new FileOutputStream(newFile);
fileWriter.write(data);
}
catch(IOException e)
{
logger.log(Level.SEVERE,"Could not write file to disk",e);
return false;
}
logger.log(Level.FINEST,"File successfully written to file");
return true;
}
The output I get after this code executes is:
WARNING: Could not create directory to place file in
So obviously Glassfish cant create this directory. I am am assuming this has something to do with permissions. Can anyone give me a direction to go as to what might be wrong here?
I am running this on Glassfish 3.12 on Ubuntu 12
different things:
1) Compare spec: (21.1.2 Programming Restrictions)
An enterprise bean must not use the java.io package to attempt to access files and directories in the file system.
I'm sure GF isn't enforcing this, but you should be aware of that.
2) The code itself is fine. Try chmod +777 on the LOCAL_FILE_DIR to get an idea if it has to do with rights in general ...
Hope that helps ...
I'm trying to run a External Jar file, without actually inserting it into my jar itself. Because the jar file needs to be located in the same folder as the main jar file.
So, Within the main jar file I want to execute the other executable jar file, And I need to be able to know when the jar file is ended AND when you close the main jar file, the jar file that is started within the jar file needs to be closed to,
I currently do that by using this code:
public void LaunchLatestBuild()
{
try {
String path = new File(".").getCanonicalPath() +
"\\externaljar.jar";
List commands = new ArrayList();
commands.add(getJreExecutable().toString());
commands.add("-Xmx" + this.btd_serverram + "M");
commands.add("-Xms" + this.btd_serverram + "M");
commands.add("-jar");
commands.add(path);
int returnint = launch(commands); //It just waits and stops the tread here. And my Runtime.getRuntime().addShutdownHook doesn't get triggerd.
if (returnint != 201) //201 is a custom exit code I 'use' to know when the app needs a restart or not.
{
System.out.println("No restart needed! Closing...");
System.exit(1);
}
else
{
CloseCraftBukkit();
Launcher.main(new String[] { "" });
}
}
catch (Exception e)
{
System.out.println(e.toString());
e.printStackTrace();
}
}
public int launch(List<String> cmdarray) throws IOException, InterruptedException
{
byte[] buffer = new byte[1024];
ProcessBuilder processBuilder = new ProcessBuilder(cmdarray);
processBuilder.redirectErrorStream(true);
this.CBProcess = processBuilder.start();
InputStream in = this.CBProcess.getInputStream();
while (true) {
int r = in.read(buffer);
if (r <= 0) {
break;
}
System.out.write(buffer, 0, r);
}
return this.CBProcess.exitValue();
}
Limitations of this code:
Doesn't close my externaljar.jar java
process on exit of the main
application.
Cannot redirect input if main console, to external jar.
That are the most Important things I need.
I hope someone can tell me how I should do this.
Current source code is available at:
http://code.google.com/p/bukkit-to-date/
Why can't you just set your classpath so that it includes the second jar, and then you can simply use it as a library ? You can even invoke the MainClass.main() method manually, if you really want that to be executed, but from within the same VM and without spawning a separate process.
EDIT: If you don't know the name of the jar file when your application is launched, but you'll only figure that out at runtime, in order to invoke it, create a URLClassLoader provided with the path to your jar file and then:
URLClassLoader urlClassLoader = new URLClassLoader(
new File("/path/to/your/jar/file.jar").toURI().toURL() );
ClassLoader cl = Thread.currentThread().getContextClassLoader();
// switch to your custom CL
Thread.currentThread().setContextClassLoader(urlClassLoader);
// do your stuff with the other jar
// ....................
// now switch back to the original CL
Thread.currentThread().setContextClassLoader(cl);
Or simply grab a reference to a class in that other jar and make use of reflection:
Class<?> c = urlClassLoader.loadClass("org.ogher.packag.ClassFromExternalJar");