Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » The code in my Java project isn't finding the imports from a referenced library
The code in my Java project isn't finding the imports from a referenced library [message #1839437] Sun, 21 March 2021 14:09 Go to next message
Inas Zadi is currently offline Inas ZadiFriend
Messages: 4
Registered: March 2021
Location: England
Junior Member
Hi, I'm writing a project that uses two classes generated by a different program (Weka generates source code for a decision tree produced when providing one of its classifiers (in my case the J48 algorithm) with training data (recording occupancy of a room (T/F) based on 5 other attributes) that I found online).
I am using Eclipse, so I created a new Java Project in the default workspace, added an external archive to its build path (as I describe below), created two empty Java classes, and pasted in the Weka-generated code. These classes import various classes from the weka.jar file (originally in the program files of the Weka software) which I already added to the project's build path, but all the import lines come up with "Import X cannot be resolved" errors (X being the imported class).

I have two versions of the project:


  • one where weka.jar is in a "lib" folder (at the same level as the "src" folder containing the two classes) which I right-clicked on in the package navigator of eclipse and selected Build Path->Add to build path
  • the other where I right-clicked on the project and selected Build Path->Add External Archives, and navigated to and selected the weka.jar file (in its original location, the program files of the Weka software) as prompted.


I've done this before in the past with success, and I'm not sure why it's not working now? I've checked the classpath variable in Eclipse and added the path to weka.jar because it wasn't there previously as suggested by someone but the imports still aren't working. I've provided some screenshots to try and help illustrate the error and what seems to me to be no issue with the location of the jar file and classpath. Thanks for any help :)

Edit: UPDATE The error is now "The package weka.classifiers.trees.j48 is not accessible" instead of the previous "Import X is not available"

[Updated on: Thu, 25 March 2021 12:47]

Report message to a moderator

Re: The code in my Java project isn't finding the imports from a referenced library [message #1839473 is a reply to message #1839437] Mon, 22 March 2021 10:10 Go to previous messageGo to next message
Sarika Sinha is currently offline Sarika SinhaFriend
Messages: 122
Registered: February 2010
Location: Bangalore, India
Senior Member
Can you check library has been added to Modulepath or Classpath? Things change with auto modules etc so you can try fixing build path.

Sarika Sinha
JDT Programmer
Re: The code in my Java project isn't finding the imports from a referenced library [message #1839482 is a reply to message #1839473] Mon, 22 March 2021 12:04 Go to previous messageGo to next message
Inas Zadi is currently offline Inas ZadiFriend
Messages: 4
Registered: March 2021
Location: England
Junior Member
Thanks for the reply :) in the attachment "Screenshot eclipse3" of my original post, I share a photo of the Classpath variable section of the Preferences window where we can see the path to the weka.jar file at the bottom.

I tried removing the jar from the classpath variable and adding it to the modulepath instead (eclipse did not allow for the same jar file to be added to both classpath and modulepath), refreshed the project and tried to run it again but the imports were still not working. I've attached screenshots of the jar file in the classpath and then the modulepath in case I did it incorrectly? I also saw there was the option to add a variable (rather than a jar) to the classpath and modulepath so I tried using those instead of the jar file directly, but this did not work either.

And after rereading your answer and seeing that you were talking about the library not the jar file, I added the library to the module path. For this I had to create a new user library which I called weka, and the imports still do not work. I've included this screenshot as well again in case I've done it incorrectly.


Edit: grammar

[Updated on: Mon, 22 March 2021 12:15]

Report message to a moderator

Re: The code in my Java project isn't finding the imports from a referenced library [message #1839488 is a reply to message #1839437] Mon, 22 March 2021 14:25 Go to previous messageGo to next message
Eitan Rosenberg is currently offline Eitan RosenbergFriend
Messages: 86
Registered: October 2018
Member
Hi,

First I want to say that I have no experience with Weka and I just wanted to see if the problem relate to Eclipse.

After downloading "Other platforms" (https://prdownloads.sourceforge.net/weka/weka-3-9-5.zip)

I found file weka-src.jar and inside was a pom.xml that point me to https://search.maven.org/artifact/nz.ac.waikato.cms.weka/weka-dev/3.9.5/jar

So I created a Maven project with Weka dependency:

<dependency>
<groupId>nz.ac.waikato.cms.weka</groupId>
<artifactId>weka-dev</artifactId>
<version>3.9.5</version>
</dependency>

I also found file wekaexamples.zip and I copied all the java files into the project.

I had to add some jars from the lib folder I found in wekaexamples.zip

All the code compiled except some warnings (raw type, deprecated method etc.) .

I use:

- Eclipse 2021-03 (4.19.0).

- openjdk-16_windows-x64_bin.zip

The pom is set to java 16:

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.target>16</maven.compiler.target>
<maven.compiler.source>16</maven.compiler.source>
</properties>

index.php/fa/40199/0/

I hope my experience will be of help

Regards.
Eitan.

Re: The code in my Java project isn't finding the imports from a referenced library [message #1839677 is a reply to message #1839488] Thu, 25 March 2021 14:38 Go to previous messageGo to next message
Inas Zadi is currently offline Inas ZadiFriend
Messages: 4
Registered: March 2021
Location: England
Junior Member
Hi, thanks for the reply :) I'm not too confident with Maven and my project is due too soon for me to get confident with it (I'm a student and it's that time of the semester where I have a lot of other deadlines too). I did however find a solution which I'll write up in another reply.
Re: The code in my Java project isn't finding the imports from a referenced library [message #1839678 is a reply to message #1839473] Thu, 25 March 2021 14:48 Go to previous message
Inas Zadi is currently offline Inas ZadiFriend
Messages: 4
Registered: March 2021
Location: England
Junior Member
Answering my own question because I found the solution: I had to check the "Defines one or more modules" checkbox in the Module Properties window of the weka.jar modulepath (accessed via the Build Path option on the dropdown from right-clicking the project). I've attached the screenshot showing so.

I do still have an error on an import of a classifier "Only a type can be imported. weka.classifiers.trees.j48 resolves to a package" which I am now trying to solve, but otherwise all my imports are now working.
Previous Topic:The jar file /usr/share/apache-tomcat-8.5.63/lib/catalina.jar has no source attachement
Next Topic:The workspace exited with unsaved changes in the previous session; refreshing workspace to recover c
Goto Forum:
  


Current Time: Fri Jun 25 11:15:17 GMT 2021

Powered by FUDForum. Page generated in 0.02414 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top