Tam
Tam

Reputation: 3997

Akka ConfigException$WrongType: myconf has type STRING rather than OBJECT

I am trying to read external conf / property file in the Akka. And I am referring this post

I have only one configuration i need to add more properties in myappConfiguration.properties. I can see properties files is able to load but not able to the read the configuration.

myappConfiguration.properties content is as follows.

myconf {
    directory : "D:\\Git\\Java_Test_WS\\file.reader\\"
}

I am getting bellow exception, Exception is clearly telling some problem of types but i did not understand what I should write.

Exception in thread "main" com.typesafe.config.ConfigException$WrongType: myappConfiguration.properties: myconf has type STRING rather than OBJECT
    at com.typesafe.config.impl.SimpleConfig.findKeyOrNull(SimpleConfig.java:159)
    at com.typesafe.config.impl.SimpleConfig.findKey(SimpleConfig.java:145)
    at com.typesafe.config.impl.SimpleConfig.findOrNull(SimpleConfig.java:172)
    at com.typesafe.config.impl.SimpleConfig.find(SimpleConfig.java:184)
    at com.typesafe.config.impl.SimpleConfig.find(SimpleConfig.java:189)
    at com.typesafe.config.impl.SimpleConfig.getObject(SimpleConfig.java:258)
    at com.typesafe.config.impl.SimpleConfig.getConfig(SimpleConfig.java:264)
    at com.typesafe.config.impl.SimpleConfig.getConfig(SimpleConfig.java:37)
    at com.myapp.file.reader.AkkaBaseEventReader.main(AkkaBaseEventReader.java:27)

Below is the Java code where I am trying to read that configuration entry. and I am getting exception in line number 27 with code :

Config config = system.settings().config().getConfig("myconf.directory");

Complete Code:

public class AkkaBaseEventReader implements ApplicationConstant {

    public static void main(String[] args) throws IOException {

        System.setProperty("config.file","myappConfiguration.properties");

        ActorSystem system = ActorSystemImpl.create("FileReaderForassignment");
        final ActorRef reader = system.actorOf(EventScannerActor.props(),"FileReader");

        System.out.println(system.settings());

        Config config = system.settings().config().getConfig("myconf.directory");       

    }
}

Below is my pom.xml that will show what version of Akka I am using :

<properties>
        <akka.version>2.4.9</akka.version>
        <maven-dependency-plugin.version>3.0.0</maven-dependency-plugin.version>
        <maven.compiler.plugin>3.6.1</maven.compiler.plugin>
        <java.compiler.target>1.8</java.compiler.target>
        <java.compiler.source>1.8</java.compiler.source>
    </properties>
    <dependencies>
        <dependency>
            <groupId>com.typesafe.akka</groupId>
            <artifactId>akka-actor_2.11</artifactId>
            <version>${akka.version}</version>
        </dependency>
        <dependency>
            <groupId>com.typesafe.akka</groupId>
            <artifactId>akka-http-core_2.11</artifactId>
            <version>${akka.version}</version>
        </dependency>
        <dependency>
            <groupId>com.typesafe.akka</groupId>
            <artifactId>akka-http-experimental_2.11</artifactId>
            <version>${akka.version}</version>
        </dependency>
        <dependency>
            <groupId>com.typesafe.akka</groupId>
            <artifactId>akka-http-jackson-experimental_2.11</artifactId>
            <version>${akka.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>${maven-dependency-plugin.version}</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <!-- This will download source so easy to see API and java doc. -->
                <artifactId>maven-source-plugin</artifactId>
                <executions>
                    <execution>
                        <id>attach-sources</id>
                        <phase>verify</phase>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <!-- Java 8 compiler plugin -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${maven.compiler.plugin}</version>
                <configuration>
                    <source>${java.compiler.source}</source>
                    <target>${java.compiler.target}</target>
                </configuration>
            </plugin>

        </plugins>
    </build>
</project>

Thanks in advance for any kind of help and information.

Upvotes: 5

Views: 11939

Answers (2)

Andreas Panagiotidis
Andreas Panagiotidis

Reputation: 3032

I had a similar issue and the reason was using the wrong quota while setting the property in Java arguments.

My wrong Java arguments

-Dlocal=true

My code

    Config config = ConfigFactory.load(Thread.currentThread().getContextClassLoader());
    config = config.getConfig("local"); //exception thrown here

The exception thrown was

Caused by: com.typesafe.config.ConfigException$WrongType: system properties: local has type STRING rather than OBJECT
at com.typesafe.config.impl.SimpleConfig.findKey(SimpleConfig.java:133)
at com.typesafe.config.impl.SimpleConfig.find(SimpleConfig.java:145)
at com.typesafe.config.impl.SimpleConfig.find(SimpleConfig.java:159)

The solution is to use single quota

-Dlocal='true'

This is happening with "com.typesafe.config" version: 1.2.1

<groupId>com.typesafe</groupId>
<artifactId>config</artifactId>
<version>1.2.1</version>

Upvotes: 2

hveiga
hveiga

Reputation: 6925

Based on your configuration the value for directory is a String and not another Config object:

myconf {
  directory : "D:\\Git\\Java_Test_WS\\file.reader\\" 
}

To get the value as a String you should do:

Config config = system.settings().config().getString("myconf.directory");

Upvotes: 4

Related Questions