0

Update 1:- Hello guys, I found some people are not able to suggest anything cuz the question is not so clear.

So I simplified the code for them and able to reproduce it again. Now all you need is just to ctrl c and ctrl v and set the property file accordingly.

package com.test;

import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.ResourceBundle;

public class ReproduceIssue {

    static {

        try {

            String propertyFilePath = "/home/jioapp/apache-tomcat-7.0.56";

            File file1 = new File(propertyFilePath);

            URL[] urls = { file1.toURI().toURL() };

            ClassLoader loader = new URLClassLoader(urls);

            ResourceBundle resourceBundle = ResourceBundle.getBundle("TinyOffline",Locale.getDefault(), loader);

            String timestamp = new SimpleDateFormat("dd.MM.yyyy-HH.mm.ss").format(new Date());

            /** Note: If file already open, moving no longer possible. */

            String logFileAbsolutePath = resourceBundle.getString("log4j.absolute.file.path");

            System.out.println(logFileAbsolutePath); /** /home/jioapp/apache-tomcat-7.0.56/OfflineLog/TinyOffline.log */

            File file = new File(logFileAbsolutePath);

            System.out.println(file);/** \home\jioapp\apache-tomcat-7.0.56\OfflineLog\TinyOffline.log */

            System.out.println(file.exists());/** false */

            if (file.exists()) {

                String renameToFile = logFileAbsolutePath + "." + timestamp;

                if (!file.renameTo(new File(
                        renameToFile))) {

                    System.out.println("Renaming log file failed : " + logFileAbsolutePath);

                }

            } else {

                System.out.println("Unable to open log file : "
                        + logFileAbsolutePath);

            }

            /** Note: If Piwik file already exist then rename it **/

            String piwikLogFileAbsolutePath = resourceBundle.getString("sys.piwik.absolute.file.path");

            System.out.println(piwikLogFileAbsolutePath);/** /home/jioapp/apache-tomcat-7.0.56/PiwikLog/access.log */

            File piwikFile = new File(piwikLogFileAbsolutePath);

            System.out.println(piwikFile);/** \home\jioapp\apache-tomcat-7.0.56\PiwikLog\access.log */

            System.out.println(piwikFile.exists());/** true */

            if (piwikFile.exists()) {

                if (!piwikFile.renameTo(new File(piwikLogFileAbsolutePath + "."
                        + timestamp))) {

                    System.out.println("Renaming piwik log file failed : "
                            + logFileAbsolutePath);

                    System.out.println("Stopping the services....");

                    System.exit(0);

                }

            } else {

                System.out.println("No Piwik access log file exist :"
                        + piwikLogFileAbsolutePath);

            }

        } catch (Exception e) {

            System.out.println(e.getMessage());

        }
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub

    }
}

TinyOffline.properties

log4j.absolute.file.path=/home/jioapp/apache-tomcat-7.0.56/OfflineLog/TinyOffline.log
sys.piwik.absolute.file.path=/home/jioapp/apache-tomcat-7.0.56/PiwikLog/access.log

I think this might help.

Java JDK-7.0.79

  • 1
    Have you tried using `System.out.println` to see what some of those strings are before you call `File.exists()`? I think this is an issue you can debug a lot more easily than we can. If one of those strings isn't what you expect and you don't know why, then you can ask a specific question about that string. – ajb Dec 13 '16 at 06:56
  • Another reason to create a [Minimal, Complete, Verifiable Example](http://stackoverflow.com/help/mcve) – you'll often find the problem yourself – qxz Dec 13 '16 at 06:59
  • Yes, I SOP file object and it is printing the file path. But still file.exists() method returning false.FYI the above code goes to a static block of my initializer class. – Satyaprakash Nayak Dec 13 '16 at 07:09
  • Hello @ajb, did you check my updated code??? – Satyaprakash Nayak Dec 14 '16 at 06:04
  • Hello @qxz , did you check my updated code??? – Satyaprakash Nayak Dec 14 '16 at 06:04
  • I can't duplicate the problem. `File.exists()` returns `true` for me, if the file does already exist. Does `/home/jioapp/apache-tomcat-7.0.56/OfflineLog/TinyOffline.log` already exist before you run the program? – ajb Dec 14 '16 at 06:17
  • @Satya You should replace the old code in your question with the new, clearer stuff instead of merely appending. (_Refine_ the question) – qxz Dec 14 '16 at 06:54
  • Yes, TinyOffline.log and access.log file should be already there. @ajb – Satyaprakash Nayak Dec 15 '16 at 07:08
  • Ok, I did that too. @ajb – Satyaprakash Nayak Dec 15 '16 at 07:12
  • BDW what version of JDK you are using?@ajb – Satyaprakash Nayak Dec 15 '16 at 07:16
  • I try the same code to run in my local system and it is running fine. The exists method returning true. But I don't understand why it is not running in my office system.@ajb – Satyaprakash Nayak Dec 15 '16 at 07:29
  • are you running on Windows or Linux (or Mac), because your following code is printing back slashes (\) System.out.println(file);/** \home\jioapp\apache-tomcat-7.0.56\OfflineLog\TinyOffline.log */ – jatanp Dec 15 '16 at 07:54
  • This one is in Windows 7, but I do run the similar type of code in Linux RedHat system and the result is same i.e. exist method returns false for TinyOffline.log and true for access.log. @jatanp – Satyaprakash Nayak Dec 15 '16 at 10:20
  • @Satya since you read the path from properties file, can check whether the special characters like : are properly escaped. Please check : http://stackoverflow.com/questions/2406975/how-to-escape-the-equals-sign-in-properties-files for more info. – jatanp Dec 16 '16 at 10:28
  • @jatanp sry I didn't check ur reply....No, there is no special character in that line. I work around many way but didn't reach to any specific answer. All I get to know that if I keep the file name "TinyOffline.properties" then it will not work but if I keep it to "tinyOffline.properties" or any thing else then it will work. – Satyaprakash Nayak Dec 19 '16 at 11:53
  • sry it's "TinyOffline.log" not "TinyOffline.properties" @jatanp – Satyaprakash Nayak Dec 19 '16 at 12:02

2 Answers2

0

It's really awkward but I just rewrite the "TinyOffline" file name of the log file and in the property file as well and it started running as per expectation. I am assuming this might be due to some junk character in the string value.

  • Did you use something like Microsoft Word to create your properties file? Don't. Be careful about what text editor you use to create files that will be read by programs, since some of them will think you're creating files that are intended to be read by humans, and they may put extra invisible stuff in the file to help with the display formatting. – ajb Dec 16 '16 at 06:15
  • No, I don't think it's bcoz of any tool. I know some ASCII char can change the whole program, but it is not at least not in my case. It is plain UTF8 file. The thing that surprises me is that if I keep the file name "TinyOffline.log" then it will not work but if I keep it to "tinyOffline.log" or anything else then it will work. Although my application runs in a mult-threaded environment. May be any daemon thread keeping lock on that file. FYI I have deleted that file and executed again but no fate. – Satyaprakash Nayak Dec 19 '16 at 12:06
-1

First of try to check whether the logFileAbsolutePath has string saved in it or not. If resourceBundle.getString is returning some value then try to check what it is returning. In my view it is not returning/storing value in object.