why are Static blocks not being executed in the NetBeans IDE (Java)? - java

I'm learning java and right now testing how static blocks work. I am trying to build and run the below code, but the static blocks are not being executed. When I compile and run the same code via the command-line (I am using command prompt(Windows 10)), the static blocks are being executed.
I am assuming this is related to some option in the IDE, but as I said I'm a still learning Java and OOP.
package statictest;
public class StaticTest {
public static void main(String args[]) {
System.out.println(Test.i);
}
}
class Test {
static int i;
static {
i = 10;
}
}
Can anyone help me out? An explanation of why this happens is also much appreciated.
PS:
When using NetBeans the output is 0
When using command-line the output is 10

You should upgrade to NetBeans 12.5.
This is a known issue in NetBeans 12.4, and has been fixed in NetBeans 12.5 (I ran a test to confirm).
Summary from the NetBeans 12.5 Features page:
NETBEANS-5832 Fixing compilation of static initializer for vanilla indexing.: https://github.com/apache/netbeans/pull/3054
The specific NetBeans JIRA ticket: Static block not compiled

i got this problem too after updating from netbeans 12.0 to 12.4, importing my old plugins fixed the problem, i think it's something related to nb-javac plugin.

Related

Static initialization block differences between Java 13 and Java 8; with different IDE versions (netbeans 8.2 vs outdated apache netbeans 12)

The only discernable difference between these two programs is the Java version.
The question is: what on earth is going on?
This image is proof that both programs contain exactly the same code, while producing different results.
here is the code:
static int x = 10;
static {
x = x + 10;
}
public static void main(String[] args) {
System.out.println(System.getProperty("java.version"));
System.out.println(new Date(System.currentTimeMillis()));
System.out.println(x);
}
LEFT side is using the old netbeans distribution.
RIGHT side is using the apache netbeans IDE.
LEFT side is using jdk8 (see image)
RIGHT side is using jdk13 (see image)
Based on the comments:
jdk13 is EOL ... but some people are unable to reproduce it while using jdk13.
The question is: what's causing this?
netbeans distribution?
borked jdk
... some setting in the apache IDE?
... the stars being out of alignemnt?
etc.
ANSWER: Underlying problem determined to exist in IDE's compilation/build/execution routine.
Reinstall and update IDE, adoption of non-EOL JDK.
Also, I did not import existing IDE settings.

Multiple main-methods for testing purposes in Netbeans 7.4 (project from Netbeans 7.2.1)

I recently switched from my older Netbeans version 7.2.1 to 7.4. I am working on a bigger project which uses only one main-entry point of course. However, for testing purposes I am using a second class which also contains a main-method. In my older Netbeans version I was able to Shift+F6 (Run File) and it did what it says: It runs the file because if has a valid main-method. With the never version of the IDE the program keeps telling me, that there is no main-method. This main-method is anything but special and the autocheck does not warn me either (Why wouldn't it? It is totally valid and worked in version 7.2.1).
Here is my testing class definition for the sake of completeness:
package Tests;
// various imports from surrounding project or external packages
public class TEST001 {
// variables and methods for further testing
public static void main(String[] args) throws IOException{
// [...]
}
}
Now, are there incompabilities between projects of Netbeans 7.2.1 to these of version 7.4 which might have caused this?
Or do I have to check a special option somewhere to allow the handling of multiple main-entry points? Which seems unlikely because running a file instead of the project seems to be permanent feature with its own user controls.
Or is this simply a bug?
Thank you for your suggestions.

How to use Caliper for benchmarking?

I am trying to figure out how to use Caliper to do benchmark testing in Eclipse and I am getting nowhere. I tried following the 26 minute tutorial found here: https://code.google.com/p/caliper/ but I get lost quickly. I have downloaded the Caliper jar file but I'm not sure what folder it should be in. I've also downloaded Maven for Eclipse plugin but I'm not even sure if that is necessary. Is it possible to install Caliper from the 'Install New Software..' option in the Help menu in Eclipse? I just want to be able to do very simple speed tests for some of the algorithms I've created for a Data Structures and Algorithms class I am taking.
This answer is now obsolete. Caliper has worked in Windows for more than a year, at least: https://code.google.com/p/caliper/issues/detail?id=167
Caliper doesn't work in Windows. See this case. You need to use version 0.5-rc1, which has other issues but is still pretty okay and is missing a lot of features, but it does work in Windows.
If you know how to use Maven, add this pom snippet to your pom.xml.
<dependency>
<groupId>com.google.caliper</groupId>
<artifactId>caliper</artifactId>
<version>0.5-rc1</version>
</dependency>
If you want to learn maven, first read this: http://maven.apache.org/guides/getting-started/maven-in-five-minutes.html
Convert your project to a maven project (Right click on project -> Configure -> Convert to Maven Project)
If you don't know how to use Maven (here is a guide to how to do this with pictures):
Download the 0.5-rc1 jar
Right click on the project you want to use and choose Build Path -> Configure Build Path
Add it to your libraries tab using Add External Jar
Once you've done that, you can start writing benchmarks. Here is an example of a benchmark I wrote for a different Stack Overflow question.
Here is how you set up a working Caliper class using the latest version of Caliper as of this writing, caliper-1.0-beta2 . As far as I can tell this procedure isn't documented anywhere outside of inline comments in the Caliper code files.
First install caliper-1.0-beta2 in pom.xml or by downloading the jar file. Then make a file like this:
import com.google.caliper.Benchmark;
import com.google.caliper.runner.CaliperMain;
public class DemoBenchmark {
public static void main(String[] args) {
CaliperMain.main(DemoBenchmark.class, args);
}
#Benchmark
void timeStringBuilder(int reps) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < reps; i++) {
sb.setLength(0);
sb.append("hello world");
}
}
}
Run that file, and Caliper will do the benchmark for you.
I have tried all the suggested solutions on Windows, however without success.
I always encountered the following error:
This selection yields 4 experiments.
ERROR: Trial failed to complete (its results will not be included in the run):
The worker exited without producing data. It has likely crashed. Inspect C:\Users\Piotr\AppData\Local\Temp\1583760443321-0\trial-1.log to see any worker output.
I was able to solve it by adding the #VmOptions annotation to a custom benchmark class.
Here is the full configuration:
#VmOptions("-XX:-TieredCompilation")
public class CaliperBenchmark {
#BeforeExperiment
void setUp() {
// set up
}
#Benchmark
void boxed() {
// test
}
}
Maven: com.google.caliper caliper 1.0-beta-2 compile
Main class:
public class Main {
public static void main(String[] args) {
CaliperMain.main(CaliperBenchmark.class, args);
}
}
Command line: mvn exec:java -Dexec.mainClass="com.google.caliper.runner.CaliperMain" -Dexec.args="org.CaliperBenchmark"

Not using any kind of javafx application still it says extend javafx

I am using jdk 1.8 , jre 1.8 and eclipse juno.Whenever i use to run my program with eclipse it gives me this error
Error: Main method not found in class A, please define the main method as:
public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application
my basic program is
public class A {
public static void main(String[] args) {
System.out.println("Hello");
}
}
Then i tried to run my program from CMD on the first time it run successfully after that it also start giving me this error..Points to remember
When i create a new project i use default jre.which is jre 8.
I am not using any kind of javaFx Application.My basic program is in front of you all
I cant every time extend javafx .So i need a strong way to resolve it.
I run a program from the command prompt for the first time it run ,but when i run another program with little bit of change it did not run.
I am stuck here guys please give me a solution for that and a little bit of explanation for that will be appreciated..
Please, close your cmd prompt.
And reopen cmd prompt.
Then open your directory containing pom.xml
To build your project, Type : mvn install
Then run the java code. For eg,
java -cp target/sample-0.0.1-SNAPSHOT.jar com.maven.sample.HelloMaven
Here HelloMaven is the class name.
I had the same error. Imported java.lang.String and the error went off.
import java.lang.String;

Why is my class not acceptable

This is my first forray into Java, and I am trying to get my head around "Hello World" using Intellij IDEA.
It's not so much the syntax I am having trouble with, more the IDE itself.
First of all, I have downloaded and installed IntelliJ IDEA, and both the 32 bit and 64 bit versions of the Java JDK. IDEA has no trouble finding my Java JDK install, and providing me with intellisense. I have created a test solution named Test, and a src directory to place my source files. My solution explorer looks like this:
My Java class is below, it compiles successfully:
public class HelloWorld {
static void main(String[] args){
System.out.println("Hello World");
}
}
I have added the Java JDK to my environmental variables on my computer, and I am able to navigate to the compiled class, and run it in command line. It runs fine.
My issue comes whenever I try and run the class from inside IDEA, for the purposes of debugging. When I click on Run, it asks me to edit my Environmental variables. In the dialogue box that appears, I select Application under Defaults, and try and select HelloWorld as my main class. I get an error telling me that HelloWorld is not acceptable, as shown below:
My question is, how do I run my Java console application inside IDEA for the purpose of debugging? What am I doing wrong?
main method should be with public modifier
public static void main(String[] args)
or even better
public static void main( final String[] args )
Dare I admit it?
I overlooked main() parameters!
main(String[] args) is of course the proper signature.
... I am pretty sure that was in Java 101.

Categories