0

I am learning Spring from this tutorial here : https://youtu.be/k0mNw-_zxrc

You can skip to 58.35 minutes as video is pretty long.

In this tutorial, instructor downloads spring dependencies (spring-beans, spring context, spring-core) in version (4.3.9) release.

And then writes this code :



package com.durgasoft.test;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.FileSystemXmlApplicationContext;

public class Test {

   public static void main(String[] args) {

      ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

      Beans obj = (Beans)context.getBean("firstBean");

      obj.print();

}

}

When I use: spring-context, spring-beans and spring-core in last version 4.3.9.RELEASE then ApplicationContext import doesn't work. It says "org.springframework.context.ApplicationContext is not accessible ". "org.springframework.context.support.ClassPathXmlApplicationContext is not accessible".

I thought I downloaded wrong jars but the same jars works fine on IntelliJ.

Note : I am not using Maven or Gradle, I am just making a simple Java project like the instructor and I added jar files manually in 'lib' folder in classpath instead of modulepath in eclipse. I am using Eclipse 2023-03 ( for Java ee developers)

And here is the exception when I run it

Exception in thread "main" java.lang.Error: Unresolved Compilation Problems:
ApplicationContext can't be resolved to a type
ClassPathXmlApplicationContext cant be resolved to a type

Any solution ?

Edit : I just noticed that there is " module-info.java" inside of src folder and it has several modules of the jars I added. All of them throws error - " spring-module can't be resolved to a module "

Edit2 : The culprit was wrong syntax in " module-info.java". Deleting it allowed me to access the import. Code compiles and runs fine now.

  • Is `import org.springframework.context.ApplicationContext;` underlined in red? Can you show the `Problems` view? Do you have a `module-info.java` file in your `src` folder? – dan1st Jun 07 '23 at 10:23
  • No " import org.springframework.context.ApplicationContext ; " isn't underlined in Red. I checked problem view but it doesn't contain any errors regarding my recent project. There's a "module-info.java" file in my src folder. Edit : I just noticed that inside " module-,info.java" there are several modules that throws error " can't be resolved to a module ". – Zeeshan Ahmad Jun 07 '23 at 10:29
  • Can you try compiling the class from the command-line? e.g. `javac -cp lib/* com/durgasoft/test/*.java` – dan1st Jun 07 '23 at 10:32
  • Does it work if you delete your `module-info.java` file (if needed you can recreate it later via right-click on project and _Configure_)? – howlger Jun 07 '23 at 11:26
  • When I tried compiling from command line following errors came - " package org.springframework.context does not exist " and " package org.springframework.context.support does not exists but I checked these ones exists in respective jars. Somehow the lib or jars are not reading them I guess. – Zeeshan Ahmad Jun 07 '23 at 12:04
  • Oh yes it worked when I deleted " module-info.java". Thanks a lot mates. And code runs fine now. I never looked at module-info.java because eclipse didn't show any error with it unless I opened it. – Zeeshan Ahmad Jun 07 '23 at 12:11
  • Maybe a missing [`requires ...;` statement in your `module-info.java` file](https://en.wikipedia.org/wiki/Java_Platform_Module_System#Properties_of_modules). – howlger Jun 07 '23 at 12:14
  • Do I need to add it now and fulfill those requirements? Or is it fine without "module-info.java" ? – Zeeshan Ahmad Jun 07 '23 at 12:17
  • `module-info.java` is only needed to make modularization more explicit or when you want to use `jlink`. – howlger Jun 07 '23 at 13:40
  • Got it. Thanks ! – Zeeshan Ahmad Jun 08 '23 at 04:47

0 Answers0