1

I know it's a cliché question and I readed all the documents and I tried many solutions but always no solution worked for me !! but I put the log4j.properties in the src/main/resources and also in src/main/java and I've added PropertyConfigurator.configure("log4j.properties");to my class but when I turn my project to a Runnable JAR it shows me :

C:\Users\eya\Desktop>java -jar RestCallAutomation.jar
log4j:WARN No appenders could be found for logger (com.infosquare.restautomation.AppController).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
log4j:ERROR Could not read configuration file [log4j.properties].
java.io.FileNotFoundException: log4j.properties (Le fichier spécifié est introuvable)
        at java.io.FileInputStream.open0(Native Method)
        at java.io.FileInputStream.open(Unknown Source)
        at java.io.FileInputStream.<init>(Unknown Source)
        at java.io.FileInputStream.<init>(Unknown Source)
        at org.apache.log4j.PropertyConfigurator.doConfigure(PropertyConfigurator.java:372)
        at org.apache.log4j.PropertyConfigurator.configure(PropertyConfigurator.java:403)
        at com.infosquare.restautomation.GenerateProcessors.start(GenerateProcessors.java:34)
        at com.infosquare.restautomation.AppController$2.run(AppController.java:98)
        at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
        at java.security.AccessController.doPrivileged(Native Method)
        at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
        at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
        at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
        at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
        at java.lang.Thread.run(Unknown Source)
log4j:ERROR Ignoring configuration file [log4j.properties].

here is the architecture of my project: maven project Architecture

and here is the log4j.properties :

# Root logger option
log4j.rootLogger=INFO, file, stdout

# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=logging.log
log4j.appender.file.MaxFileSize=10MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

here ismy pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.OpenText</groupId>
  <artifactId>GenerateurCodes</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>

  <dependency>
  <groupId>org.apache.maven</groupId>
  <artifactId>maven-model</artifactId>
  <version>2.2.1</version>
</dependency>
  <dependency>
    <groupId>org.jdom</groupId>
    <artifactId>jdom</artifactId>
    <version>1.1</version>
   </dependency>
 <dependency>
    <groupId>com.googlecode.json-simple</groupId>
    <artifactId>json-simple</artifactId>
    <version>1.1</version>
 </dependency>
 <dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.8.5</version>
</dependency>
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.2</version>
</dependency>
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpmime</artifactId>
    <version>4.5.2</version>
</dependency>
<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.5</version>
</dependency>
<dependency>
    <groupId>org.apache.velocity</groupId>
    <artifactId>velocity</artifactId>
    <version>1.7</version>
    </dependency>
<dependency>
     <groupId>org.apache.velocity</groupId>
     <artifactId>velocity-tools</artifactId>
     <version>2.0</version>
</dependency>
<dependency>
    <groupId>org.w3c</groupId>
    <artifactId>dom</artifactId>
    <version>2.3.0-jaxb-1.0.6</version>
</dependency>
<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>

 </dependencies>
</project>

**UPDATE : **

when I open the jar using WinRar it shows me that it contains the log4j.properties and log4j jar so it s not a resources problem!

MAryem
  • 182
  • 1
  • 2
  • 14
  • Could you please share your pom.xml as well, and also please think about how you can provide an [MCVE](http://idownvotedbecau.se/nomcve/) per the help article [How to Ask a Good Question](http://stackoverflow.com/help/how-to-ask) – D.B. May 16 '20 at 22:07
  • Have you added `resource` folder [configuration](https://maven.apache.org/plugins/maven-resources-plugin/examples/resource-directory.html) in your pom.xml? – Mpac May 16 '20 at 22:20
  • @Mpac this can be added automatically by eclipse : right clic on the project --> Properties -->Java build path -->Sources and you choose your sources – MAryem May 16 '20 at 22:22
  • @D.B. I added my pom.xml can you solve my problem now ? – MAryem May 16 '20 at 22:33
  • Why is there no `build` section in your pom? This element is very important, you should probably study maven documentation some more, I don't think your pom is doing what you want it to do. – D.B. May 16 '20 at 22:38
  • there is no need to add the build part in the project "pom.xml" because you can do it manually using eclipse : right clic on the project --> Properties -->Java build path -->Sources and you choose your sources ! even when I right click on the jar and open it using winrar it shows me that log4j.properties does exist ! – MAryem May 16 '20 at 22:43

2 Answers2

1

first of ALL : It called : log4j.properties not Log4j.properties !!

Second : instead of PropertyConfigurator.configure("log4j.properties"); it should be : PropertyConfigurator.configure(getClass().getResource("/log4j.properties"));

MAryem
  • 182
  • 1
  • 2
  • 14
0

Check your resource directory in maven build. It should be something like this.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
.......
     <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
            </resource>
        </resources>
     </build>
</project>

This way log4j.properties file will be in the classpath directory.

  • no it showed me : `Exception in Application start method Exception in thread "main" java.lang.reflect.InvocationTargetException Caused by: java.lang.NullPointerException: Location is required.` – MAryem May 16 '20 at 22:13
  • Now, it's a different error. Maybe the problem exists in your code where you are trying to read some file from location. – Dharmendra Vishwakarma May 16 '20 at 22:18
  • this error did not appear befor adding the part – MAryem May 16 '20 at 22:20
  • That's the reason you are getting this error as the previous error got resolved. The error is similar to https://stackoverflow.com/questions/20507591/javafx-location-is-required-even-though-it-is-in-the-same-package. Try to explore/open your build after maven build locally and check if .fxml file still exists after build process in the class path. – Dharmendra Vishwakarma May 16 '20 at 22:23
  • I updated my question ! always my problem is : `log4j:ERROR Could not read configuration file [log4j.properties]. java.io.FileNotFoundException: log4j.properties (Le fichier spécifié est introuvable)` – MAryem May 16 '20 at 22:32
  • The answer remains the same. You need to include build step to tell maven to copy resources in the classpath. – Dharmendra Vishwakarma May 16 '20 at 22:37
  • when I right clic on the runnable jarand open it using winrar it shows me that log4j.properties exists ! – MAryem May 16 '20 at 22:40