简体   繁体   中英

java maven import issue: error: package com.google.gson does not exist

I have a new environment, and I'm trying to make a simple java project in IntelliJ containing a ScoreCalculator.java class that will import com.google.gson. This should be simple but there's something I'm missing.

I made a fresh maven project (including this dependency in pom.xml), and when I try javac ScoreCalculator.java I get: error: package com.google.gson.stream does not exist

Reading around, I found the jar file where this son-2.8.6.jar file lives (inside my .m2 directory), and I updated my CLASSPATH to be that folder path:

Dianes-MBP:java dkaplan$ echo $CLASSPATH
/Users/dkaplan/.m2/repository/com/google/code/gson/gson/2.8.6

My pom file has:

<?xml version="1.0" encoding="UTF-8"?>
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>mightier-maven</artifactId>
    <version>1.0-SNAPSHOT</version>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>7</source>
                    <target>7</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
<dependencies>
    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.8.6</version>
    </dependency>
</dependencies>
</project>

My little test program so far has nothing but the imports I'll need:

import java.io.File;
import java.io.FileReader;
import java.nio.file.Path;
import com.google.gson.*;
import com.google.gson.stream.JsonReader;

public class ScoreCalculator {
    public static void main(String args[]){

        System.out.println("Hello world");
    }
}

The directory setup looks like this: 目录设置看起来像这样

But when I try compiling this way I get the error:

Dianes-MBP:java dkaplan$ javac ScoreCalculator.java
ScoreCalculator.java:5: error: package com.google.gson.stream does not exist
import com.google.gson.stream.JsonReader;
                             ^
ScoreCalculator.java:4: error: package com.google.gson does not exist
import com.google.gson.*;
^
2 errors

Note: I also tried the same thing with a fresh Gradle project in IntelliJ and I get the same error from the same command, so I'm thinking there's some setup (or directory organization?) piece I'm missing here.

What else should I do/check?

从 MacOS 中ScoreCalculator.java所在的目录执行这些命令。

javac -cp /Users/dkaplan/.m2/repository/com/google/code/gson/gson/2.8.6/gson-2.8.6.jar ScoreCalculator.java

java -cp /Users/dkaplan/.m2/repository/com/google/code/gson/gson/2.8.6/gson-2.8.6.jar ScoreCalculator

use this dependency

   <dependency>
        <groupId>org.json</groupId>
        <artifactId>json</artifactId>
        <version>20090211</version>
    </dependency>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM