Eclipse autocompletion for jdk.* packages does not work - java

I write here because I noticed a strange behaviour of Eclipse IDE (2022-12).
Basically, the autocompletition does not work properly when I use a class from a jdk.* package.
For example:
import jdk.jshell.*;
public class Test {
private static final JShell JSHELL = JShell.create();
...
...
public static void main(String[] args) {}
}
Now when I write JSHELL. (e.g., inside the main method), I get only proposal for the methods of the Object class. No specific proposal is available.
What am I doing wrong?
Thanks.
Tried updating & reinstalling Eclipse.

jdk.* is filtered by default.
When you want to use something of jdk.* in a project, go to the preferences Java > Appearance > Type Filters and uncheck the item jdk.*.

Related

#com.sun.istack.internal.NotNull annotation

I try to use the #NotNull annotation from package com.sun.istack.internal.
I am using IDE Intellij IDEA Community Edition.
when I build a program using IDE no problem. When I try to compile a file from the command line using javac, I get an error "cannot find symbol".
package ibkr;
import com.sun.istack.internal.NotNull;
public class Main {
public static void main(String[] args) {
test("Test");
}
public static void test(#NotNull String text) {
System.out.println(text);
}
}
I don't understand why i can't compile this code using javac and how Intellij IDEA make compilation and run it.
The annotation is an internal class, it's not for public use. The closest alternative is Jetbrains' stuff:
https://www.jetbrains.com/help/idea/nullable-and-notnull-annotations.html
Even if the class is in the classpath, it doesn't necessarily mean that you can safely refer to that class. The compiler can have some limitations upon accessing some classes/packages.
In most cases, as here, it's obvious whether the package is internal or not: com.sun.istack.internal. Oracle discourages developers from using classes from such packages.
Ok i know this question is a bit old, but if my info is correct, the reason for this is the fact that intellij uses full rt.jar for compilation while javac uses incomplete version, because of ct.sym
This is an annotation used to identify non-nullable values, also this will let static analyzer have their checks in place. In case you are using IntelliJ you can use its annotation but it would make it very tool-specific, same is the case for eclipse
One can also you
https://mvnrepository.com/artifact/com.sun.xml.bind/jaxb-impl
This provides the same non-null annotation but you need to add an extra dependency there.

Cannot Run Class Files

Notice that the run option for the main class is greyed out. I tried restarting intelliJ, rebuilding, cleaning, everything.
(click to zoom in)
Move your code inside of the src folder. Once it's there, it'll be compiled on-the-fly every time it's saved.
IntelliJ only recognizes files in specific locations as part of the project - namely, anything inside of a blue folder is specifically considered to be source code.
Also - while I can't see all of your source code - be sure that it's proper Java syntax, with a class declared the same as the file and that it has a main method (specifically public static void main(String[] args)). IntelliJ won't run code without a main method (rather, it can't - neither it nor Java would know where to start).
Use
public static void main(String[] args))
Your main method requires a String array or varargs argument to be picked up.
Correct your main() method, from:
public static void main() {...}
to
public static void main(String[] args) {...}

Java template for a project in Eclipse

Is it possible to make creation of (Java) file(s) in Eclipse easier/quicker.
I know there is that "Create new class wizard", but it is slow for my "special" needs...
I have a specific project in which I'm creating new classes often, but the structure for those classes is the same. Let say I want to create class A, so I want file A.java to be created as
class A {
public static void main(String[] args) {
}
static int solve() {
}
}
and it would be perfect that also ATest.java is created for this class, for example
class ATest {
#Test
int test1() {
Assert.assertEquals(0, A.solve());
}
}
or is there such plugin for Eclipse?
Yes, you can try using FastCode Plugin, where you can create new templates as per your requirement. As FastCode plugin supports custom templates, here is an example how to create the above class and test class together:
<template name="CREATE_NEW_CLASS">
<description>Used to Create class in the specified package.</description>
<allowed-file-names>*.*</allowed-file-names>
<first-template-item>package</first-template-item>
<second-template-item>none</second-template-item>
<additional-parameters>className</additional-parameters>
<template-body>
<![CDATA[
<fc:class type="class" name="${className}" package="${package.name}" project="${package.javaProject}">
public class ${className} {
public static void main(String[] args) {
}
static int solve() {
}
}
</fc:class>
<fc:class type="test" name="${className}Test" package="${package.name}" project="${package.javaProject}">
public class ${className}Test {
#Test
int test1(){
Assert.assertEquals(0, ${className}.solve());
}
}
</fc:class>
]]>
</template-body>
</template>
Once you add the template, you need to do import using import option in template preference page as explained in the document.
Yes, this is relatively simple to set up. Open the Project Properties and navigate to the Java Code Style > Code Templates section. Once there, check the box to enable project-specific settings. The generation template you want is under the Code part of the tree; you want Class Body. It is probably empty, but click the Edit... button to modify it.
Whatever you enter in the Edit dialog will be inserted between the class' brackets when using the New Class wizard.
There's no way I know of to automatically create another class (the Test in your case). But Eclipse has a JUnit wizard that makes doing so very easy. Just right-click on a class and choose New > Other... and then find Junit Test Case in the list. That wizard will guide you through creating the test class, including selecting the method(s) you want to test.
Note: these instructions set up the template for just the project or projects you select. You could also set up the same thing for your entire workspace Preferences, but doing so provides no way to share that configuration so that the same project checked out into another workspace will use it. I usually recommend making these kinds of settings on a per-project basis.
I'm not sure you need an IDE-specific template for that. When I was going through Project Euler, I had a setup like this:
public interface Problem {
public Object solve();
}
public class MyProblem implements Problem {
#Override
public Object solve() {
// do some stuff
return result;
}
}
Then in your (JUnit?) tests, you could use
Assert.assertEquals(expected, myProblemInstance.solve());
You can see my implementation here
If you do use an IDE template, you won't be able to use a generic solve() method, since it is not guaranteed that that class has that method. I would highly recommend using interfaces.

Eclipse "Run as" has no "Java Application" option (What exactly is needed to be able to run a class/.java?)

I want to run a specific .java file (a class), but Eclipse is not co-operating.
I have tried to follow the instructions here -->
eclipse how to run a different class
... but my "Run As" menu item never contains "Java Project".
I have tried accessing the menu after right-clicking in the .java pane and tab itself, and from the .java name and class name in the Package Explorer, but of course that doesn't make a difference. The only option I ever get is "Run Configurations".
(and yes, my .java has a "main" method.)
import com.jsyn.JSyn;
public class SuperSimpleSounds {
public static void main() {
[...]
What, exactly, is needed to be able to run an individual class (an individual .java file)?
Add a String array argument to the main method as expected by the JVM
public static void main(String[] args) {
Also one trick when you want to open some java classes that you downloaded elsewhere is to create a new Java project. Then, move all your java classes into the src folder of the project. Now you can run with the option of "run as Java Application".
As a minor variation on the perfect answer of Reimus:
Add a String array argument to the main method as expected by the JVM
public static void main(String[] args) {
I had been struggling with my a script, when I ran it showed Run Configuration and Fail.
By changing the following:
public static void LaunchBrowser () throws InterruptedException {
to
public static void main(String[] args) throws InterruptedException {
it solved the problem.

How to run Java .class file from another .class file? (java newb)

I've been running different individual Java .java files in the Netbeans IDE by right-clicking the .java files themselves in the Project Explorer of Netbeans (the portion normally at the upper left part of Netbeans).
However, i've been googling on how to make a class file run another class file using code, but to no avail.
I have a project named "loadanotherfile" with 2 files, namely: Loadanotherfile.java and otherfile.java
I'm trying to make Loadanotherfile.java run otherfile.java, but I'm not exactly sure how. I read about Classloaders and URLClassloaders however these methods don't seem suitable for my purpose of running another .java file.
Below is the code of the 2 files i mentioned.
Loadanotherfile.java
package loadanotherfile;
public class Loadanotherfile {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
System.out.println("Hello World!");
// TODO code application logic here
}
}
otherfile.java
package loadanotherfile;
public class otherfile {
public static void main(String args[])
{
System.out.println("This is the other file.");
}
}
I have a feeling that the task has something to do with using the "import" syntax (namely something like import loadanotherfile.* but even if my guess is correct, I'm still not sure on how to make my Loadanotherfile.java run otherfile.java using code.
How can I load otherfile.java using Loadanothefile.java?
Cheers
In Loadanotherfile.java
otherfile.main(args);
Compile the two together, and then from Loadanotherfile,
otherfile.main(args);
will do the trick. You don't need to import since you're in the same package. Note the linked tutorial.
I would investigate (however) class instantiation, and creating an instance of a new class to invoke upon. Invoking static methods from static methods isn't very OO.
Try This:
className.main(Args){
}
This works! ive tested it myself.
Check the public void main line. If there IOException and not there then insert
in Loadanotherfile.java
use this
otherfile.main(args);{
}

Categories

Resources