Class not found: "md.leonis.ServiceMagi?Test" - java

I am writing a simple Java test using JUnit and constantly get the following error: Class not found: "md.leonis.ServiceMagi?Test". I don't see any stacktrace.
Recommendations for this ticket did not help: Class Not Found Exception when running JUnit test
I invalidated the InteliJ Idea cache, recompiled the project, with no result. Example class:
public class ServiceMagiсTest {
#Test
public void test() {
assertTrue(true);
}
}
Who faced this before?

I figured it out myself. For some mysterious reason, one letter in the class name has been replaced with a similar letter from the Russian alphabet. IDE gave a hint:
Identifier contains symbols from different languages: [LATIN, CYRILLIC]
Non-ASCII characters in an identifier
Hope this helps someone.

Related

How to run a java .class file that extends a Class which is in another directory?

I first met this problem in my Ideas, I wrote a Class that extends javax.servlet.http.HttpServlet, My Ideas throws an error message reads Error: Could not find or load main class com.bjpowernode.OneServlet, here is the image:
enter image description here
It can be seen that Idea didn't show the red wavy line, that shows my codes are fine. I found a solution to this problem from enter link description here, I changed the scope from provided to compile:
enter image description here
But I actually want to know why and how it works? I compared the difference of Idea compilation instructions under different scope setting,I found that when Idea uses java command to run the .Class, the parameter -classpath of the command of the compile scope has two more paths:
D:\apache-tomcat-8.0.50\lib\jsp-api.jar;D:\apache-tomcat-8.0.50\lib\servlet-api.jar
That's to say, Idea didn't consider external library paths when run .class under the provided scope, and the super Class HttpServlet is from servlet-api.jar package. Why?
To simplify the problem, I created two different classes under two different paths and packages: Class Base and Class Sub, and Sub extends Base.
The codes of the Base is here:
package base;
public class Base{
public static void main(String[] args) {
}
}
The codes of the Sub is here:
package sub;
import base.Base;
public class Sub extends Base{
public static void main(String[] args) {
System.out.println("In Sub");
}
}
The path of Base is ./path1/base/Base.java and the path of Sub is ./path2/sub/Sub.java.
I compiled them using these two commands:
javac ./path1/base/Base.java -d ./path1
javac ./path2/sub/Sub.java -d ./path2 -cp "./path1;./path2"
And compiled successfully. But when I run sub.Sub using the command below:
java sub.Sub -cp "./path1;./path2"
And I got the same error:
Error: Could not find or load main class sub.Sub
Ive tried multiple variations of this, but none of them seem to work. Any ideas? Although I solved the problem of idea reporting errors, I still could not understand the principle behind? I hope this question can help me to figure it out. My jdk version is 1.8. Thanks in advance.

#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.

JUnit #Test InvalidStateException

I'm having a problem with my test application which is follows. I'm having following code:
#Test(expected = InvalidStateException.class)
public void testLiianPitkaKayttajatunnus()
{...}
I'm using Eclipse and it says "InvalidStateException cannot be resolved to a type" and as we know if eclipse notice a problem it underline it with red wave line. I have been trying to search solutions but still not found any. I have tried following things but none of them has solved the problem:
Adding org.apache.openjpa dependency to my pom.xml and adding line before class code "import org.apache.openjpa.persistence.InvalidStateException;"
In thread, it is said that if you have in your class extension of other class, it should be taken out. This didn't helped neither.
In the same thread one advice is to use JUnit 4.4 and it didn't helped neither.
So my question is how this problem can be solved?
For additional information, my class start follows (imports are not mentioned here)
public class TietorakenneTest extends TietokantaTestCase {....} and JUnit version that I use is 4.10,
any help is appreciated. If you need any other information about the code, please let me know and I will put them here.
Qualify the class by adding the package name.
#Test(expected = org.apache.openjpa.persistence.InvalidStateException.class)
Change your import statement to
org.apache.openjpa.util.InvalidStateException and as you mentioned add org.apache.openjpa dependency to my pom.xml
Hope it helps.

Classes in same package

I love the Intellij IDEA but i have been stacked on one little problem with Java imports.
So for example there is a package with name "example" and two different classes in it: A.java and B.java.
And i wanna get access to class "A" from class "B" without imports. Like this:
class A:
package example;
public class A{ ... some stuff here ...}
class B:
package example;
public class B{
public static void main(String[] args){
A myVar = new A();
}
}
This code may not work, but it's doesn't matter. Trouble just with IDE and with its mechanism of importing classes.
So, problem is that i can't see A class from B. Idea says 'Cant resolve symbol' but i actually know that class A exists in package. Next strange is that complier works fine and there are no exceptions. Just IDEA can't see the class in the same package.
Does anybody has any ideas?
If they are in the same package, you can access A in class B without import:
package example;
public class B{
public static void main(String[] args){
A myA = new A();
}
}
Maybe this will help you, or somebody else using IntelliJ that is getting a "Cannot resolve symbol" error but can still compile their code.
Lets say you have the two files that buymypies wrote up, the standard Java convention is that the two files would exist in an Example directory in your source code area, like /myprojectpath/src/Example. But it is technically not a requirement to reflect the package structure in the source directory structure, just a best practice sort of thing.
So, if you don't mimic the package structure, and the two files are just in /myprojectpath/src, IntelliJ will give you the "Cannot resolve symbol" error because it expects the source code structure to reflect the package structure, but it will compile okay.
I'm not sure if this is your problem, but I do use IntelliJ and have seen this, so it's something to look at.
I have the same problem as this: 2 classes in the same package, yet when one tries to call the other, Intellij underlines it in red and says Cannot resolve symbol 'Classname', e.g. Cannot resolve symbol 'LocalPreferencesStore'.
It then wants to add the fully qualified name in situ - so it clearly knows the path - so why can't it just access the class?
The module still compiles and runs, so it's just the IDE behaving oddly - and all that red is very distracting, since it isn't actually an error, it's just IDEA throwing a weird wobbly.
This is also recent. Two weeks ago I wasn't having this problem at all, and now suddenly it's started up. Of course, it could go away again on its own soon, but it's really annoying.
Same issue and I just fixed it.
I don't know your folder structure.
However, if your package example was added manually.That's the problem.
The package should be the same as your folder structure,which means if you want your class file to be stored in package example,you must store you java file in the src's subfolder named example.
You need to learn the basics about Java i think.
Here is a basic example of what i think you are trying:
package Example;
public class A
{
String myVar;
public String getMyVar()
{
return myVar;
}
public void setMyVar(String myVar)
{
this.myVar = myVar;
}
}
You need to create an instance of A.
package Example;
public class B
{
/**
* #param args
*/
public static void main(String[] args)
{
// TODO Auto-generated method stub
A myA = new A();
myA.setMyVar("Hello World!");
System.out.println(myA.getMyVar);
}
}
Look up java 'getters' and 'setters'.

Is Java not installed correctly? Exception in thread "main" java.lang.NoSuchMethodError

Recently when I write any code and compile it, then try to run it I get this exception:
Exception in thread "main" java.lang.NoSuchMethodError
At first I thought there is something wrong in my code, but I couldn't find anything wrong with it. When trying to run a HelloWorld example that had worked before, if works, but if I copy the exact same code into a file HelloWorld2 I get this exception again.
The code is identical but when I used javap to decompile both class files I found a difference. In HelloWorld (the original file)
"public static void main(java.lang.String[])";
and in HelloWorld2 (the new one)
"public static void main(String[])";
without java.lang..
I recompiled the old HelloWorld with javac and now when I try to run it it doesn't work and I get the same exception. None of my old code now works if I recompile it.
I've searched everywhere but can't find a solution to this problem - any idea what is going on here?
You may get this if you have your own class called String (without a package) in your classpath. It sounds like that's what happened. Here's a way to try to reproduce this - compile it and run it, and see if it looks the same:
class String {}
public class Test {
public static void main(String[] args) {
}
}
Once you've got the compiled String.class file in your file system in an awkward place, that will be used by default even if you only compile the Test class above...
Basically, see if you can find a file called String.class somewhere.

Categories

Resources