Java enum gives exception - java

I have enum DebugMode in a separate file DebugMode.java.
package core;
public enum DebugMode {
Disabled, Console, Dialog, All
}
Then I have a class Debug in Debug.java.
package core;
public class Debug {
private static DebugMode debug = DebugMode.All;
public static void Message(String str) {
//some unrelated stuff
}
}
But for some reason it gives me error to this string:
private static DebugMode debug = DebugMode.All;
The error is:
Caused by: java.lang.RuntimeException: Uncompilable source code - class DebugMode is public, should be declared in a file named DebugMode.java
at core.DebugMode.<clinit>(Debug.java:6)
... 3 more
Java Result: 1
I just don't understand what's the problem... can someone help me figure it out?

Related

Could not find or load java main class

I have a very basic program in java,the filename is MainClass1.java
import java.util.Scanner;
class Student{
String name;
int roll_number;
double marks;
Student(String s,int r,double m)
{
this.name=s;
this.roll_number=r;
this.marks=m;
}
}
class MainClass1{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
String name="Sourav";
int roll_number=25;
double marks=70.50;
Student student1=new Student(name,roll_number,marks);
System.out.println("The student is "+student1.name+" and his roll number is "+student1.roll_number+" and his marks is "+student1.marks);
}
}
It compiles fine,however when I am trying to run it by
java Mainclass1
it shows an error
Error: Could not find or load main class Mainclass1
Caused by: java.lang.NoClassDefFoundError: MainClass1 (wrong name: Mainclass1)
I know its a very basic issue,but unable to figure out the issue.
Please help anyone
When you run a program (that is, when you call java ...) the JVM needs to know where to start running the code. It looks for a public static method named main, which takes an array of String as input, and is defined on whatever class is named in the .java file.
So:
verify that your file is named "MainClass1.java" – the filename and the class name need to match
verify that the class includes the public modifier, so change this:
class MainClass1 {
...
}
to this:
public class MainClass1 {
...
}

Calling Java Code in C# using IKVM results in System.TypeInitializationException error

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.

- Syntax error on token ".", # expected after this token

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.

CodeNameOne - NativeInterface - ClassNotFoundException on NativeLookup

I followed the instructions from this video https://www.codenameone.com/how-do-i---access-native-device-functionality-invoke-native-interfaces.html, but my code is throwing java.lang.ClassNotFoundException: interfaces.MyNativeImpl on the look up line. The code I'm using is almost exactly that of the video.
package interfaces;
import com.codename1.system.NativeInterface;
public interface MyNative extends NativeInterface{
public String sayHi();
}
in the android native directory
package interfaces;
public class MyNativeImpl {
public String sayHi() {
return "hi";
}
public boolean isSupported() {
return true;
}
}
and in the java code:
MyNative my = (MyNative)NativeLookup.create(MyNative.class);
if(my != null && my.isSupported()){
System.out.println("Hello!");
}
Where did I go wrong now?
That is perfectly fine code and will work on the device.
You are seeing the exception in the simulator because the native/internal_tmp directory is missing from your runtime classpath but it shouldn't cause a problem other than no native interfaces on the desktop:

java.lang.ClassNotFoundException when instantiating an inner class

I have a following problem with an innner class. Here's the code:
public class PGZUserManagerBean {
// joomla login as separate thread
private class JoomlaLogin extends Thread {
private AuthJoomla authJoomla;
public JoomlaLogin(AuthJoomla authJoomla){
this.authJoomla = authJoomla;
}
#Override
public void run(){
this.authJoomla.authJoomla();
}
}
public void validateuser(){
AuthJoomla authJoomla = new AuthJoomla();
JoomlaLogin joomlaLogin = new JoomlaLogin(authJoomla);
joomlaLogin.start();
}
}
I'm getting java.lang.ClassNotFoundException: PGZUserManagerBean$JoomlaLogin on runtime. I'm using Java 1.6.
Thank you for the help in advance.
al
I strongly suspect that you've copied the class files from one place to another (or put them in a jar file) but you've failed to copy/include PGZUserManagerBean$JoomlaLogin.class.
Check where you're running the code, and look for the class file that the JVM can't find. It will definitely be in your compilation output.

Categories