My Eclipse worked fine a couple of days ago before a Windows update. Now I get error messages whenever I'm trying to do anything in Eclipse. Just a simple program as this will display a bunch of error messages:
package lab6;
public class Hellomsg {
System.out.println("Hello.");
}
These are the errors I receive on the same line as I have my
"System.out.println":
"Multiple markers at this line
- Syntax error, insert ")" to complete MethodDeclaration
- Syntax error on token ".", # expected after this token
- Syntax error, insert "Identifier (" to complete MethodHeaderName"
You can't just have statements floating in the middle of classes in Java. You either need to put them in methods:
package lab6;
public class Hellomsg {
public void myMethod() {
System.out.println("Hello.");
}
}
Or in static blocks:
package lab6;
public class Hellomsg {
static {
System.out.println("Hello.");
}
}
You can't have statements outside of initializer blocks or methods.
Try something like this:
public class Hellomsg {
{
System.out.println("Hello.");
}
}
or this
public class Hellomsg {
public void printMessage(){
System.out.println("Hello.");
}
}
You have a method call outside of a method which is not possible.
Correct code Looks like:
public class Hellomsg {
public static void main(String[] args) {
System.out.println("Hello.");
}
}
Just now I too faced the same issue, so I think I can answer this question.
You have to write the code inside the methods not on the class, class are generally used to do some initialization for the variables and writing methods.
So for your issue, I'm just adding your statement inside the main function.
package lab6;
public class Hellomsg {
public static void main(String args[]){
System.out.println("Hello.");
}
}
Execute the above, the code will work now.
Related
I've included my code below. Following some other examples, I even tried to dynamically load the class in order to force it to run the static block, but that doesn't solve my problem. The class is loaded and class.getName() is printed successfully, but still, when it gets to the last line in the main method it throws an error saying the array is null.
All the other answers address things which don't seem to apply here, like how using the "final" keyword can allow the compiler to skip static blocks. Any help is appreciated!
package helper;
public class StaticTest {
public static boolean [] ALL_TRUE;
private static void setArray(){
ALL_TRUE = new boolean[8];
for(int i=0;i<ALL_TRUE.length;i++){
ALL_TRUE[i] = true;
}
}
static {
setArray();
}
public static void main(String [] args){
ClassLoader cLoader = StaticTest.class.getClassLoader();
try{
Class aClass = cLoader.loadClass("helper.StaticTest");
System.out.println("aClass.getName() = " + aClass.getName());
} catch(ClassNotFoundException e){
e.printStackTrace(System.out);
}
System.out.println(StaticTest.ALL_TRUE[0]);
}
}
In case anyone else lands here, the problem was that I had checked the Netbeans option "Compile on Save" (under Build->Compiling). Somehow, compiling files immediately upon saving was preventing the static block from being run.
Again, thanks to everyone who chimed in to verify that the code itself worked as expected.
I am currently trying to call Java Code in C#. One possibility is IKVM, whereupon I looked at a tutorial for this tool. I have to say, and that's really curious: the tool seems to work in part.
But now to my problem:
So I took the following tutorial (https://www.codeproject.com/Articles/594632/IKVM-NET-in-Details). Following this example, I wrote my own Java code. In addition, I have added a few more methods to the java file. My source code for testing is relatively short:
The Java source code:
package TestProject;
public class TestClassJava {
public static void Print() {
System.out.println("Hi C# from JAVA");
}
public static void PrintStr(String str) {
System.out.println(str);
}
public static String returnString() {
return "Hi C# from Java method";
}
public static String returnInputString(String input) {
return input;
}
public static int retInt() {
return 42;
}
public static int returnIntNumber(int inp) {
return inp;
}
public static boolean returnTrueBoolean() {
return true;
}
}
The C# source code:
using System;
using System.IO;
using TestProject;
using ikvm.io;
using ikvm.lang;
using ikvm;
using ikvm.runtime;
using ikvm.extensions;
namespace IKVM_Test_Case_08_08_2019
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(TestClassJava.retInt()); // shows: 42 (works!)
Console.WriteLine(TestClassJava.returnString()); // shows: Hi C# from Java method (works!)
TestClassJava.Print(); // here appears the error System.TypeInitializationException
TestClassJava.PrintStr("Hallo"); // here appears the error System.TypeInitializationException
Console.WriteLine(TestClassJava.Print()); // can not convert from void to bool
}
}
}
The whimsical part now happens while running the program in C#. I try the methods in C# via Console.WriteLine(TestClassJava.retInt()); then, for example, the number 42 will be given to me, as it should be. I can also call the method returnString().
In the methods without return value, however, Print() & PrintStr(String str), I always get the following error message:
Error message:
System.TypeInitializationException
HResult=0x80131534
Message=The type initializer for 'java.lang.StdIO' threw an exception.
Source=IKVM.OpenJDK.Core
StackTrace:
at java.lang.System.get_out()
at TestProject.TestClassJava.Print()
at IKVM_Test_Case_08_08_2019.Program.Main(String[] args) in C:\Users\...\source\repos\IKVM_Test_Case_08_08_2019\IKVM_Test_Case_08_08_2019\Program.cs:line 19
Inner Exception 1:
MissingMethodException: Method not found: 'Void System.IO.FileStream..ctor(System.String, System.IO.FileMode, System.Security.AccessControl.FileSystemRights, System.IO.FileShare, Int32, System.IO.FileOptions)'.
I can not quite explain that, so I asked this question in the hope, that someone knows an advice.
According to the quoted tutorial, it all had to work that way. Nevertheless, I get this error message.
I hope my question is so far understandable.
I Have three classes
StaticHolder.java - Which holds a static variable.
StaticInitializer.java -Responsible only for initializing the variable through a static method.
Application.java - Retrieves the static variables value through getter method.
I thought initializing a static variable once in JVM will not go until we stop the JVM. So I called ran StaticInitializer once which will do the initialization. And tired to access its value from another class which is not working and returning null. Can anyone explain why. Thanks In Advance.
public class StaticHolder {
private static String hello;
public static void ini() {
hello = "Hello World";
}
public static String getHello() {
return hello;
}
public static void setHello(String hello) {
StaticHolder.hello = hello;
}
}
class StaticInitializer {
public static void main(String[] args) {
StaticHolder.ini();
while (true) {
Thread.sleep(1000);
}
}
}
public class Application {
public static void main(String[] args) {
System.out.println(StaticHolder.getHello());
}
}
static does not mean that this value is there forever!
It is only theree for the current java session.
Invocing the java command at the command line starts a new java session where the value needs to be initialized again.
Actually I have a daemon thread which does the initialization and stays alive.And I have another stand alone java program which tries to get the value.
Without knowing that other code involved my gueass is that you did not establish inter process communication.
The easiest way it that you "deamon" opens a server socket and your "stand alone java program" connects to it an queries the desired data through it.
So there is only one main method that can be executed as entry point for the entire application for each JVM run.
When the JVM is executed you can specify which class has to be loaded at start. The Classloader take care to load that class and then the JVM can execute the only one public static void main(String[] args) method.
In Java you need to have at least one class with a public static method named main. I suggest to read this post to understand why it is public static.
The Java Classloader is a part of the Java Runtime Environment that
dynamically loads Java classes into the Java Virtual Machine.
Usually classes are only loaded on demand.
So returning to your question, given that when Application.main is running there is no way to execute StaticHolder.init(), I suggest to change your main in this way:
public class Application {
public static void main(String[] args) {
StaticHolder.init();
System.out.println(StaticHolder.getHello());
}
}
or change StaticHolder in this way and remove the init:
public class StaticHolder {
private static String hello;
static {
hello = "Hello World";
}
public static String getHello() {
return hello;
}
public static void setHello(String hello) {
StaticHolder.hello = hello;
}
}
On the other hand, just to be clear if you run the StaticInitializer.main this has no effect on Application.main execution.
In your program , when main method of StaticInitializer is first executed, a String named hello is initalized. and as ini() method is called, the value 'Hello world' is assigned to hello. Then jvm exists main method, and then stops working. Again when we compile application class,instead of the previous hello variable , a new hello string variable is created with no value assigned(null valued) . That's why you're getting null as output. Thankyou.
Please refer this image find errors and give me the proper solutions for the same.
You can't use System.out.println() for any class properties.
Declare any method and inside that use the println() to print anything.
Look at the following example:
public class Test {
static String string;
//the following println() will create the error: Multiple markers at this line
//- Syntax error on token(s) bla bla bla
System.out.println("hello");
public void printSomething() {
//But this is ok
System.out.println("hello");
}
public static void main(String[] args) {
Test test = new Test();
test.printSomething(); // prints hello
}
}
I am getting a strange error while creating a simple thread program in JAVA using Eclipse. The code is:
package threadshow;
public class Thread_Show extends Thread{
public void run(){
System.out.println("Inside the thread");
}
}
class Thread_Definition{
Thread_Show ts=new Thread_Show();
ts.start(); //Getting the error here
}
I am getting error "syntax error on token start identifier expected" in the line ts.start();. Why am I getting this?
EDIT I have used the code from http://tutorials.jenkov.com/java-concurrency/creating-and-starting-threads.html#thread-subclass
Found a very bad mistake done my me. Forgot to add public static void main(String args[]) in the Thread_Definition class.
You can't start your method inside class. Create some method first.
Are you defining both of your classes in the same java file?. If so, you define both the classes in different java files naming Thread_show and Thread_definition. Then inside Thread_definition you can create an object of Thread_show and call its function.
ADD main method-public static void main(String[] args)
package threadshow;
public class Thread_Show extends Thread
{
public void run()
{
System.out.println("Inside the thread");
}
}
class Thread_Definition
{
public static void main(String[] args)
{
Thread_Show ts=new Thread_Show();
ts.start();
}
}