import package.* to import methods in eclipse - java

I'm looking at some examples of lwjgl 3. In their example code they import:
import static org.lwjgl.glfw.GLFW.*;
I've seen this crop up many times now and I thought I'd just get round to asking. The code then goes on to lines such as:
glfwDestroyWindow(window);
Where it calls the function in the GLFW class. However, in my IDE (eclipse luna) this does not work as intended, I would need to call:
GLFW.glfwDestroyWindow(window);
Which isn't really a problem, just an inconvenience. How is it that I can get eclipse to recognise that this function is within the class imported so I do not have to direct it.

The sample code in the getting started is dependent on newer features of lwjgl 3, which means that if you download the (as of 2014-12-05) stable build from the lwjgl download page, the code will not compile because classes referenced in the code are not present - the entire org.lwjgl.glfw package is missing from the stable download.
You must download one of the nightly builds in order to get the required functionality; this will most likely be resolved once the v3 of the library is fully released.

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);

Why is Java RunTime has reported a java.lang.NoSuchMethodError on Hex.encodeHex() method call?

I had to include some bittorrent java library to my Android project. My workspace: Android Studio 1.0.2 (osx) and jdk8. I've connected its maven-repository (ttorrent:1.4) with Gradle and after starting using main classes and features i've got an error:
java.lang.NoSuchMethodError: No static method encodeHex([BZ)[C in class Lorg/apache/commons/codec/binary/Hex; or its super classes (declaration of 'org.apache.commons.codec.binary.Hex' appears in /system/framework/ext.jar).
I went to library's code and find out that it's using org.apache.commons.codec from where ttorrent is importing encodeHex and calling it. Looks like binaryHex method is gone! Or it never been. But I went to commons.codec's code and found binaryHex in its place and with arguments that I was looking for. How come? Why? My Android Studio found it. But java runtime not.
In fact, the decision was more difficult than I thought. Let's start with the fact that I came across an article by Dieser Beitrag'a, from which it is clear that not one I had similar problems. The whole thing turned out that within the Android operating system already has some libraries that have a higher priority use, rather than loaded with dependencies along with the application. Among them there and my org.apache.commons.codec.
Yes, such things.
To solve the problem in two ways, either you need to pump source code library and using some tool to rename the project (i.e. org.apache.commons.codec to org.apache.commons.codec.android), collected it to a .jar file, include .jar in a project and at code use imports of the necessary classes only "our" library, or just get the required class to your project and do not pull a megabytes of unneeded code. However, I did just that.
Thanks for help!

Importing anything from "net" in eclipse

I am relatively new to java as a disclaimer. I see all kinds of code examples where people "import net...." yet anytime that I try to import anything from this directory, I get an error that the compiler cannot resolve the import net. What have these other programmers done that allows them to use this import? I have seen other people having this problem but I have not seen a straight-forward answer to this question.
For instance:
import net.sourceforge.binge.Xbox360Controller;
When you import an external package, you need to have it in your program during runtime, which means that it either has to exist in YOUR project, or if it is being loaded by another program, then that program has to contain it. To add a library to your eclipse project: see THIS

SelendroidDriver not in jar file

I have recently written a Selenium program in Java that works perfectly with the FireFoxDriver(). My step 2 is to be able to run this program on my Android device with Selendroid. I went on their website here and have been able to download the jar and connect to their localhost with port:4444.
However, when I try their example, Eclipse doesn't recognize the SelendroidDriver() and suggests me to go back to WebDriver().
Here is their code:
SelendroidCapabilities capa = new SelendroidCapabilities("io.selendroid.testapp:0.10.0");
// My error appears when I create the new SelendroidDriver().
WebDriver driver = new SelendroidDriver(capa);
WebElement inputField = driver.findElement(By.id("my_text_field"));
Assert.assertEquals("true", inputField.getAttribute("enabled"));
inputField.sendKeys("Selendroid");
Assert.assertEquals("Selendroid", inputField.getText());
driver.quit();
I have also noticed that the SelendroidDriver class does not appear in my files although I downloaded the Selendroid jar file, version 0.10.0.
For those of you who are curious about how I fixed my problem, here is what I did:
I went to this site to get the selendroid-client jar file corresponding to the standalone version I had.
Downloading the standalone jar file was somehow not enough.
I was facing the same problem till I found the SelendroidDriver class here. Import this and change the package name according to your project.
SelendroidDriver.java has classes implementing interface methods, which in Java 1.6 can be annotated with #Override. However, in Java 1.5, #override could only be applied to methods overriding a superclass method.
Go to your project preferences and set the "Java compiler level" to 1.6 and also make sure you select JRE 1.6 to execute your program from Eclipse.
After adding this class, you would still see multiple dependency errors, but now in the SelendroidDriver.java file. You can import these classes now to counter these errors. Ensure that the package hierarchies are maintained correctly, in accordance with the GitHub directories and your working project.
After importing all these classes, the constant fields SWITCH_TO_CONTEXT, GET_CONTEXT_HANDLES and GET_CURRENT_CONTEXT_HANDLE were not being resolved. I used a poor workaround of changing them to some other available constant field for testing this sample.
I struggled a lot with this error and finally came to know that the paths have been changed in the latest releases. Use these paths and it shall work:
import io.selendroid.common.SelendroidCapabilities;
import io.selendroid.client.SelendroidDriver;
Tested this for versions 0.15.0 & 0.17.0

Java app suddenly not compiling with jdev

I have been working on an inherited code base that is a Java web app and just today it stopped compiling. I am getting errors that it can't find some classes that are declared in the code.
My code base is set up like this: I've got the main package and then, for some reason, both inside it and at the same level of it, I've got a .jar that holds a supplemental package, com.oreilly.servlet to be exact. I have some files in my main code base that import com.oreilly.servlet.MultipartRequest and utill today they had no problem finding them.
I have since wiped my local version and checked out the last revision which I know compiled last timed I made any changes to the java files, leading me to believe my issue is jdeveloper.
The errors I am getting are that package com.oreilly.servlet doesn't exist and subsequently that it cannot find class MultipartRequest.
I have also broken out the jar file (which by the way is included int he Libraries and Classpath section of jdev) into a package structure to no avail.
Any help would be much appreciated.
Sounds like an opportunity to update and refactor to me. I stumbled across the com.oreilly package years ago, haven't seen it since. I say remove it from the code and upgrade to the more standard javax.servlet classes (usually found in a servlet-api.jar or something similarly named bundled with your web app server).

Categories