1

When I tried to use java to connect my local server in Intellij, I got this error. I don't know what mistake it is, which part it comes from. Can anyone enlighten me? I already got the postgresql jdbc driver in libs.

When I run it with intellij, it said:

/Users/alvinpeng/.sdkman/candidates/java/11.0.8.hs-adpt/bin/java -javaagent:/Users/alvinpeng/Library/Application Support/JetBrains/Toolbox/apps/IDEA-C/ch-0/202.7319.50/IntelliJ IDEA CE.app/Contents/lib/idea_rt.jar=51001:/Users/alvinpeng/Library/Application Support/JetBrains/Toolbox/apps/IDEA-C/ch-0/202.7319.50/IntelliJ IDEA CE.app/Contents/bin -Dfile.encoding=UTF-8 -classpath /Users/alvinpeng/Desktop/program/out/production/classes:/Users/alvinpeng/.gradle/caches/modules-2/files-2.1/com.google.guava/guava/28.2-jre/8ec9ed76528425762174f0011ce8f74ad845b756/guava-28.2-jre.jar:/Users/alvinpeng/.gradle/caches/modules-2/files-2.1/com.google.guava/failureaccess/1.0.1/1dcf1de382a0bf95a3d8b0849546c88bac1292c9/failureaccess-1.0.1.jar:/Users/alvinpeng/.gradle/caches/modules-2/files-2.1/com.google.guava/listenablefuture/9999.0-empty-to-avoid-conflict-with-guava/b421526c5f297295adef1c886e5246c39d4ac629/listenablefuture-9999.0-empty-to-avoid-conflict-with-guava.jar:/Users/alvinpeng/.gradle/caches/modules-2/files-2.1/com.google.code.findbugs/jsr305/3.0.2/25ea2e8b0c338a877313bd4672d3fe056ea78f0d/jsr305-3.0.2.jar:/Users/alvinpeng/.gradle/caches/modules-2/files-2.1/org.checkerframework/checker-qual/2.10.0/5786699a0cb71f9dc32e6cca1d665eef07a0882f/checker-qual-2.10.0.jar:/Users/alvinpeng/.gradle/caches/modules-2/files-2.1/com.google.errorprone/error_prone_annotations/2.3.4/dac170e4594de319655ffb62f41cbd6dbb5e601e/error_prone_annotations-2.3.4.jar:/Users/alvinpeng/.gradle/caches/modules-2/files-2.1/com.google.j2objc/j2objc-annotations/1.3/ba035118bc8bac37d7eff77700720999acd9986d/j2objc-annotations-1.3.jar program.App
Unable to load the class. Terminating the program

Process finished with exit code 255

When I tried to run it with gradle, it said:

The client will now receive all logging from the daemon (pid: 2592). The daemon log file: /Users/alvinpeng/.gradle/daemon/6.4/daemon-2592.out.log
Starting 7th build in daemon [uptime: 1 hrs 55 mins 42.971 secs, performance: 96%, non-heap usage: 22% of 268.4 MB]
Using 8 worker leases.
Starting Build
Settings evaluated using settings file '/Users/alvinpeng/Desktop/program/settings.gradle'.
Projects loaded. Root project using build file '/Users/alvinpeng/Desktop/program/build.gradle'.
Included projects: [root project 'program']

> Configure project :
Evaluating root project 'program' using build file '/Users/alvinpeng/Desktop/program/build.gradle'.
All projects evaluated.
Selected primary task 'App.main()' from project :
Tasks to be executed: [task ':compileJava', task ':processResources', task ':classes', task ':App.main()']
Tasks that were excluded: []
:compileJava (Thread[Execution worker for ':',5,main]) started.

> Task :compileJava UP-TO-DATE
Caching disabled for task ':compileJava' because:
  Build cache is disabled
Skipping task ':compileJava' as it is up-to-date.
:compileJava (Thread[Execution worker for ':',5,main]) completed. Took 0.018 secs.
:processResources (Thread[Execution worker for ':',5,main]) started.

> Task :processResources NO-SOURCE
Skipping task ':processResources' as it has no source files and no previous output files.
:processResources (Thread[Execution worker for ':',5,main]) completed. Took 0.0 secs.
:classes (Thread[Execution worker for ':',5,main]) started.

> Task :classes UP-TO-DATE
Skipping task ':classes' as it has no actions.
:classes (Thread[Execution worker for ':',5,main]) completed. Took 0.0 secs.
:App.main() (Thread[Execution worker for ':',5,main]) started.

> Task :App.main() FAILED
Caching disabled for task ':App.main()' because:
  Build cache is disabled
Task ':App.main()' is not up-to-date because:
  Task has not declared any outputs despite executing actions.
Starting process 'command '/Users/alvinpeng/.sdkman/candidates/java/11.0.8.hs-adpt/bin/java''. Working directory: /Users/alvinpeng/Desktop/program Command: /Users/alvinpeng/.sdkman/candidates/java/11.0.8.hs-adpt/bin/java -Dfile.encoding=UTF-8 -Duser.country=AU -Duser.language=en -Duser.variant -cp /Users/alvinpeng/Desktop/program/build/classes/java/main:/Users/alvinpeng/Desktop/program/build/resources/main:/Users/alvinpeng/.gradle/caches/modules-2/files-2.1/com.google.guava/guava/28.2-jre/8ec9ed76528425762174f0011ce8f74ad845b756/guava-28.2-jre.jar:/Users/alvinpeng/.gradle/caches/modules-2/files-2.1/com.google.guava/failureaccess/1.0.1/1dcf1de382a0bf95a3d8b0849546c88bac1292c9/failureaccess-1.0.1.jar:/Users/alvinpeng/.gradle/caches/modules-2/files-2.1/com.google.guava/listenablefuture/9999.0-empty-to-avoid-conflict-with-guava/b421526c5f297295adef1c886e5246c39d4ac629/listenablefuture-9999.0-empty-to-avoid-conflict-with-guava.jar:/Users/alvinpeng/.gradle/caches/modules-2/files-2.1/com.google.code.findbugs/jsr305/3.0.2/25ea2e8b0c338a877313bd4672d3fe056ea78f0d/jsr305-3.0.2.jar:/Users/alvinpeng/.gradle/caches/modules-2/files-2.1/org.checkerframework/checker-qual/2.10.0/5786699a0cb71f9dc32e6cca1d665eef07a0882f/checker-qual-2.10.0.jar:/Users/alvinpeng/.gradle/caches/modules-2/files-2.1/com.google.errorprone/error_prone_annotations/2.3.4/dac170e4594de319655ffb62f41cbd6dbb5e601e/error_prone_annotations-2.3.4.jar:/Users/alvinpeng/.gradle/caches/modules-2/files-2.1/com.google.j2objc/j2objc-annotations/1.3/ba035118bc8bac37d7eff77700720999acd9986d/j2objc-annotations-1.3.jar program.App
Successfully started process 'command '/Users/alvinpeng/.sdkman/candidates/java/11.0.8.hs-adpt/bin/java''
Unable to load the class. Terminating the program
:App.main() (Thread[Execution worker for ':',5,main]) completed. Took 0.123 secs.
2 actionable tasks: 1 executed, 1 up-to-date

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':App.main()'.
> Process 'command '/Users/alvinpeng/.sdkman/candidates/java/11.0.8.hs-adpt/bin/java'' finished with non-zero exit value 255

here is my program's error

get_connection function:

get_connection function

main function:

main function

CrazyCoder
  • 389,263
  • 172
  • 990
  • 904
Alven Peng
  • 17
  • 3

1 Answers1

0

JDBC driver jar is not in the classpath.

I already got the postgresql jdbc driver in libs

If you have it in the local directly for the Gradle based project you also need to adjust build.gradle file to include the local jars into classpath like described here:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
}

However, it should be much easier to just include the repository dependency so that Gradle can download and configure the driver automatically.

dependencies {
    compile group: 'org.postgresql', name: 'postgresql', version: '9.4-1206-jdbc42'
}

Not sure what JDBC driver version you need for this project. You can check this link for all the available versions and adjust the dependency in build.gradle.

Don't forget to reimport the project so that IDE knows about the new dependencies.

For the projects not using Gradle/Maven the JDBC jar needs to be added to the module dependencies manually.

CrazyCoder
  • 389,263
  • 172
  • 990
  • 904
  • Hi sir, thank you for your help, but I still got the same error even if I add dependencies in build.gradle, it said: Process 'command '/Users/alvinpeng/.sdkman/candidates/java/11.0.8.hs-adpt/bin/java'' finished with non-zero exit value 255 – Alven Peng Oct 08 '20 at 04:40
  • Please share the [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). – CrazyCoder Oct 08 '20 at 05:17
  • Your app is coded to exit with `-1` code on any error. Try printing the exception before every exit (or remove all the try/catch blocks and add the exceptions to the main method signature), then check the output for the exception stacktrace so that you can better understand the actual error. – CrazyCoder Oct 08 '20 at 05:27