#Java doesn't see Maven dependency

66 messages · Page 1 of 1 (latest)

heavy epoch
#

I've switched from Eclipse to VSCode. In eclipse I've been using classpath on project and everything worked great. I wasn't adding anything into the project.
Now I want to use maven. I added MSSQL JDBC into pom.xml as well as maven compiler and jar plugin. If I want to start my program, I can do it with no poblems.

The problem is with the jdbc dependency. Java doesn't see it. How can I use it in project?

hidden juniperBOT
#

This post has been reserved for your question.

Hey @heavy epoch! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

hollow frigate
heavy epoch
#

No, it's not

#

I can do mvn clean and mvn install with no problems

hollow frigate
#

But if you run it, it doesn’t run

heavy epoch
#

My code works, it just doesn't see JDBC

#

I wrote an error I want to get if I can't connect to SQL db, and I get it each time I try it. Message from exception is No suitable driver found for.... I had it in eclipse as well, but I just used Add external JARs in project properties

#

Now I want to use solely maven approach. I've been trying to do this for couple days already. I see that they add dependecy to pom.xml file and doesn't show their code, or they use their own project as a dependency which wuoldn't work for me (here they do show their pom.xml and code example).

royal glen
#

Why did you switch to VSC?

#

In Eclipse, you would need to reload the Maven project - I assume it's similar for VSC (if VSC supports Maven)

heavy epoch
#

VSC supports maven through plugin. I can do the same thing using mvn command. The reason I switched was to learn maven on a bare example.
I couldn't get maven to work in eclipse too so I just used classpaths there too and that's what worked there

royal glen
#

Eclipse should be able to work with Maven but ok

heavy epoch
#

I'm back on eclipse.
I removed JDBC from external jars, added maven dependency, mvn clean, mvn generate sources, mvn install. I still get No suitable driver found (on eclipse and on diferent project usign same jdbc)

royal glen
#

you need to reload the Maven project

#

Alt+F5

#

or right click -> Maven -> Update Project

heavy epoch
#

No changes

royal glen
#

Did that open a menu?

heavy epoch
#

Update project? Yes.
I had to choose which project I want to update

royal glen
#

Can you show your pom.xml?

heavy epoch
#
<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>amounter</groupId>
  <artifactId>amounter</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>amounter</name>
  
  <dependencies>
    <!-- https://mvnrepository.com/artifact/com.microsoft.sqlserver/mssql-jdbc -->
    <dependency>
        <groupId>com.microsoft.sqlserver</groupId>
        <artifactId>mssql-jdbc</artifactId>
        <version>9.4.1.jre8</version>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.5.1</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.4</version>
        <configuration>
          <archive>
            <manifest>
              <mainClass>amounter.Demo</mainClass>
            </manifest>
          </archive>
        </configuration>
      </plugin>


    </plugins>
  </build>

</project>
royal glen
#

and mvn install ran successfully?

#

because it seems like you forgot <mainClass> in the <manifest> entry

heavy epoch
#

Yes, it runs succesfully each time

#

It creates a jar which I have to open with command java -jar, but I later exported it from eclipse choosing my main classand I could later run it with double press

royal glen
#

Can you show your DriverManager.getConnection statement?

royal glen
heavy epoch
#
try(Connection Conn = DriverManager.getConnection(nazwaBazy, uzytkownik, haslo)){
...
heavy epoch
#

The jar that mvn install creates in target folder can run, but I need to use java -jar

royal glen
#

so you can run it from Eclipse but not with java -jar?

#

That's because the Maven JAR plugin doesn't include dependencies

heavy epoch
#

I can run both ways. Both ways has the same problem with No suitable driver found

#

I added <scope>runtime</scope> to the dependency, updated, cleaned, installed, in that order, with same result.

#

I think that the project can't see the maven dependency

royal glen
#

Yeah I know

#

so the maven-jar-plugin doesn't include dependencies in the JAR

#

if you want to have a JAR with dependencies, you need to use the maven assembly plugin

heavy epoch
#

Even if I want to run it from eclipse?

royal glen
#

Eclipse doesn't use the JAR

#

but you said it worked from Eclipse..?

#

or did I misunderstand you?

heavy epoch
#

By run I mean start my program. Compile it and run.
Both ways does that. Eclipse's Ctrl+F11 works the same as java -jar amounter.jar command. They both start my program adn I get the same thing that I coded in whenever connection cannot me established.

#

Okay. So it all was my fault. I did a typo in URL.

royal glen
#

On the left in the package explorer

#

oh lol

#

this should fix it in Eclipse but the JAR will still not work

heavy epoch
#

Last time I used maven the JDBC dependency was in pom.xml but the connection wouldn't start

heavy epoch
heavy epoch
#

I don't know if taht makes sense. I could run maven's jar file with maven-jar-plugin and maven-compiler-plugin. I jsut had to use java -jar command, because double pressing the jar file does java command

#

And java command needs a class file, while I'm trying to run jar file so I need to sue -jar parameter

royal glen
#

That is correct

#

or almost correct

#

technically, double-clicking runs javaw and not java - but that just means there is no console

heavy epoch
royal glen
#

Did you add the assembly plugin?

heavy epoch
#
<plugin>
 <artifactId>maven-assembly-plugin</artifactId>
 <version>2.5.3</version>
 <configuration>
  <descriptorRefs>
   <descriptorRef>jar-with-dependencies</descriptorRef>
  </descriptorRefs>
 </configuration>
 <executions>
  <execution>
   <id>create-archive</id>
   <phase>package</phase>
   <goals>
    <goal>single</goal>
   </goals>
  </execution>
 </executions>
</plugin> 
#

That's what I added

#

The <mainClass>... tag is i maven jar plugin

royal glen
#

you need it in the assembly plugin

heavy epoch
#

To be sure, should I remove the maven-jar-plugin?

royal glen
#

you can but you don't have to

heavy epoch
#

I added mainClass to assembly plugin and removed maven-jar-plugin. I still have to use -jar parameter.
Thanks for your help. It all boiled down to a stupid typo in a code.