java class import shows compile error - java

I am trying to make some changes to a legacy code of a plugin which was written using Java version 4. I am trying to extend a class from an imported package.
import org.eclipse.wst.xml.core.internal.document.XMLModelContext;
public class XMLModelContextForPma extends XMLModelContext
{
}
I'm quite new to plugin development. I couldn't figure out why the compiler shows
The type org.eclipse.wst.xml.core.internal.document.XMLModelContext is not visible error. Also, most of the codes in classes of this particular package are using .internal. packages which are giving Discouraged access warnings. I'm googled here and there and found it's because of non-standard/API classes.
But this is quite strange. I have the jar files in the build path but not sure what is wrong here.
I'm developing in Eclipse Juno, Mac OS X, Java 6

It looks like the class XMLModelContext is private or protected and in a different package.
If a class is declared as protected, you can only use it in other classes within the same package or any of it's sub packages.

Add that jar(org.eclipse.wst.xml.core.internal.document.XMLModelContext containing jar) to your project file path.

Related

Eclipse not able to see javax package

I'm currently trying to fiddle with images, specifically convert images from JPEG, WEBP, and BMP forms to PNG forms and my method uses the javax.imageio.ImageIO class. When I tried importing it, Eclipse yelled that the package that the type was not accessible. I thought that was weird and went digging through StackOverflow on my own and found multiple answers saying I should remove and re-add the JRE. This didn't work, somewhat unsurprisingly, but while looking through my build path I noticed that the JRE was missing the entire javax package. Is there a reason this could be? Is there a fix?
The exact error reads The type javax.imageio.ImageIO is not accessible and the suggested edits ask me if I want to make class ImageIO in package javax.imageio.
I am using the latest build of Eclipse. My JDK is java-16-openjdk-amd64. I am running Ubuntu 20.04. I built this app from the ground up, so I am not using Maven (unless Eclipse uses Maven by default).
I tried compiling a basic class in my command line and it worked for some reason, despite not working in Eclipse.
I would rather not revert my JDK to an older version if I don't have to.
It turns out I was being just being an idiot. It turns out I had actually made this with a module without realizing it. All it took for me was to get rid of the module file.
You do not call "new" on a static class
To make an instance non static of it if it ever does have such a type available from one of its static methods you cast it to that type.
However, with the javax.imageio.ImageIO you make other classes from its methods.
import java.awt.image.BufferedImage;
import java.io.*;
try{ // wrap in FileNotFoundException IOException
File input = new File("/somewhere/over/the/rainbow/cementplant.jpg");
//static classes are called directly with a method
BufferedImage bfi = (BufferedImage)javax.imageio.ImageIO.read(input);

Eclipse throws the error "Data cannot be resolved to a type" for using the Project Lombok [duplicate]

This question already has an answer here:
Eclipse Java IDE JUnit5: junit.jupiter.api.Assertions is not accessible
(1 answer)
Closed 2 years ago.
I am new to Java and Project Lombok, I am trying to follow some tutorials to understand it better. I have created a very simple java project to test the Project Lombok and it throws the error Data cannot be resolved to a type when I add the annotation #Data to my class.
I am using macOS and Eclipse IDE for running my Java application. Following are the steps that I followed to run my first Java and Lombok project:
Downloaded the latest Project Lombok version 1.18.16 and placed it in the Documents folder.
Installed the Project Lombok in my system using the command java -jar lombok.jar.
Restarted my Eclipse and verified that Project Lombok installed in the Eclipse -> About Eclipse: "Lombok v1.18.16 "Envious Ferret" is installed. https://projectlombok.org/"
Created a new Java project and the class Alien.
Added the Project Lombok JAR to my project Classpath.
Added the #Data annotation to my Alien class and imported the JAR file but I get the error The type lombok.Data is not accessible for my import statement import lombok.Data;
Also, I get the error Data cannot be resolved to a type for my annotation at the class level public #Data class Alien
I am really unsure what's wrong. I tried to find out but all the answers have mentioned the same steps. I was just wondering what am doing wrong. Any help would be really appreciated.
Following is my complete code:
package com.lamboktest;
import lombok.Data;
public #Data class Alien {
private int age;
private String name;
}
The error The type SOMETYPE is not accessible is... bizarre. As far as I know, there are only three ways to get that error:
lombok.Data isn't public. Except, it is, so this can't be it.
You've manually added a list of forbidden packages ('access rules') in eclipse, to deny yourself access to lombok. Presumably, you'd remember if you did that, so it can't be this.
lombok is loaded as a module, and the lombok package isn't exported. Except, the only versions of lombok that exist either aren't modularized (and module-info-less jar files export everything), or they are, and the lombok package is exported. So it can't be this.
Perhaps it would be useful to doublecheck all of these:
I guess javap -cp lombok.jar lombok.Data, and check that it's public, but, this just cannot be it, so you probably don't need to bother.
For that access rules thing, which is the most likely of 3 very unlikely options, go into your project's setup (right click on it in e.g. the package explorer, and pick 'properties...', you can set up lots of stuff from there), go to 'build path', find the lombok.jar, open it up, and check 'access rules'. It should read 'no rules defined'.
Go into your project's setup and ensure it's all set to java8, and rebuild. Presumably you want to use new features, but the point is to at least isolate down to: It's a module issue.
Posting the answer as it maybe useful for somebody in the future.
As mentioned in the comment removed/deleted the file module-info.java which was created by default during the project creation. Everything worked as expected.

Integrating a Java Class into a PyDev Project (Jython) in Eclipse

I have a Java class that i would like to import into my Jython script. Unfortunately, eclipse won't let me create a Java class inside my Jython project.In the window, where you create and name your Java class, I get a message at the top (alongside a red cross) saying, "Source folder is not a Java project" when I type the name of the would be class. How do I rectify this? I need the Java Class to call C code using the JNI (declaring the native method,loading and then calling it). Thank You !!!!!!!
What you can do is to create second module which would be java project. Anyhow, logically it should be that way. Please check out other similar question - PyDev: Jython modules & Java classes in the same project.
Other links that might help - http://pydev.org/manual_101_project_conf2.html
So what nefo_x suggested is correct. You need to create a new Java project that will contain your Java class. Then import the Java package as you would a python module. But there are a few things to watch out for in eclipse to make it work. I list the whole process below:
Your Java class (or classes) should not be in the default package. You need to create a new package and make/put your java class files there.
Export the package as a jar file to some place on your computer.
Add the jar file (located at some place on your computer) to your python path.
Import the package by writing "import PackageName".
The problem for me was that I had my java class in the default package. That does not work due to some naming issues. Anyhow, hope that this helps.

How to import a class from a package in a different folder?

I am new to java. This is a basic question about packages. I have a small java project named "stacklist.java" in Netbeans IDE. It's package name is stacklist. And it has 4 different classes. One of them is ListNode.
Now i need ListNode object in other project "queuelist.java".
directory structure is StackList->src->stacklist and QueueList->src->queuelist. Both StackList and QueueList are at the same level.
And added the folder(StackList\src) in Libraries of queuelist.java project. I did "import stacklist.*;"
When i run "clean and build project", i am getting this: "error: package stacklist does not exist
import stacklist.*;"
Please suggest me.
For
package a.b.c;
public class D;
package e;
import a.b.c.D;
public class E;
you need
src\a\b\c\D.java
src\e\E.java
You might go for Maven, a popular professional build infrastructure which helps with libraries from the internet and library versioning. And programming conventions.
For maven:
package a.b.c;
public class D;
package e;
import a.b.c.D;
public class E;
you need
src\main\java\a\b\c\D.java
src\main\java\e\E.java
Developing two projects needs care. If one project gives a library StackList.jar then you need to keep this library builded up to date. Often an IDE takes a shortcut, but the explicit use of a library may yield version errors.
Adding StackList.jar file and removing the folder(StackList\src) from the Libraries of current project made this to run without errors.

Why are my classes not found even though my imports are correct?

The Classes ADSTool and the Class PPM are not found by my compiler. I'm using Eclipse Helios. The docs say that I have to import java.lang.Object for both, and I imported java.lang*; I can't see why this isn't working.
The docs say that I have to import java.lang.Object for both, and I imported java.lang*
Any documentation that tells you that you have to import classes in java.lang is blatantly wrong. The classes in java.lang are implicitly imported.
In fact, I suspect that your problem is that you need to add the JAR files containing those two classes (ADSTool and PPM) to the build path of your Eclipse project. If the classes are not available on the build path, the Eclipse compiler won't find them.
check Project->Properties->Java Build Path
did you include JRE System Library in Libraries Tab?
Import java.lang.*, not java.lang*. The second dot matters; without it, the compiler can get confused about what you're actually trying to import.

Categories