I have the following code, which needs to be run by the HackerRank automated validator.
package stringrev;
import java.util.Scanner;
class str {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
int k=in.nextInt()+1;
for(int i=0;i<=k;i++)
{
StringBuffer a=new StringBuffer(in.nextLine());
StringBuffer b=a.reverse();
System.out.println(b);
}
}
}
This code is working fine on my compiler but while uploading to Hacker Rank it shows an error:
Error: Could not find or load main class str
What does that mean?
HackerRank, as well as other automated websites, will take your code snippet and run it inside of another program (the automated validator).
The error you are getting is due to the fact that the validator tries to compile / access your code from within his code. Unfortunately, you have setup the visibility of your Str class to be default, which is not public!
If you change your code to:
public class str {...}
It should work. Also check that the name of the class is correct (it's unlikely that they ask you to have a lowercase class name).
This error might happen if you add the package of the class, when you're copying the code from your editor to the Hacker Rank. As an example, I had this error,
package hackerrank.RoadTransformation;
import java.util.*;
public class Solution{
...
}
I removed the package hackerrank.RoadTransformation; part and the error was gone.
Related
i got a run-time error on executing a file 'complement.java' that read
D:\java\files\java complement
Error:Could not find or load main class complement
the class structure follows as below
import java.io.*;
class billion
{
.
.
.
//class definition
}
what could be the cause for the prompt mentioned above?
First of all, this question can be easily answered by 5 seconds of quick googling, supposing you dont know java.
Anyway, the error only shows when your main class is missing a main method, which start the whole program. The main method is
public static void main(String[] args) {}
Add this to your main class, and start it again.
File name and class name need to be same. And the class you mention in java command should have a main method.
public static void main(String[] args) {}
I've faced an issue with compilation, but cannot understand why it occurs.
Actually much time was spent to understand where the reason is (it was far from obvious in a "crap" project), but after reproducing that error I greatly simplifies all the code to show a little example especially for you:
Package structure:
com.company
|
----Main.class
|
----maker
|
----Maker.class
Maker.class
package com.company.maker;
public interface Maker {
}
Main.class
package com.company;
import static com.company.Main.MakerImpl.Strategy.STRATEGY1;
import static com.company.Main.MakerImpl.Strategy.STRATEGY2;
import com.company.maker.Maker;
public class Main {
public static void main(String[] args) {
System.out.println(STRATEGY1.name() + STRATEGY2.name());
}
static class MakerImpl implements Maker {
enum Strategy {
STRATEGY1, STRATEGY2
}
}
}
And I got compilation error in Main class:
Error:(15, 39) java: cannot find symbol
symbol: class Maker
location: class com.company.Main
And if I change the import sequence from
import static com.company.Main.MakerImpl.Strategy.STRATEGY1;
import static com.company.Main.MakerImpl.Strategy.STRATEGY2;
->import com.company.maker.Maker;
to
->import com.company.maker.Maker;
import static com.company.Main.MakerImpl.Strategy.STRATEGY1;
import static com.company.Main.MakerImpl.Strategy.STRATEGY2;
then it is compiled successfully.
Is it normal behaviour of Java Compiler? If so I want to clearly understand why it happens.
P.S. tested using java version 1.8.0_112 and 1.7.0_80 (MacOS)
check this :
http://bugs.java.com/bugdatabase/view_bug.do?bug_id=6391197
It seems that the compiler sees the first static import and then jumps to take care of your inner class, but fails because it did not read the last non static import.
So when you change the import order, this problem does not occur, since when the compiler reads the static import and jumps to take care of the inner class because those imports are used in the inner class, the compiler is happy since it already imported the non static Maker Interface.
Wich means that the imports are evaluated eagerly.
I hope this helps.
See also Java how import order matters when import class/enum inner an inner class to see that this problem also exists with non static imports.
I have looked through many answers to similar questions. But couldn't narrow down to a solution.
Following is the code: (Simplifying names for readability)
First class:
package p1;
public class C1 {
public static void test() {
System.out.println("Boom!");
}
}
Second class:
package p2;
import p1;
public class C2 {
public static void main(String[] params) {
C1.test();
}
}
Clean-Build doesn't give any error. (No compilation error)
But at runtime I'm getting following error:
Exception in thread "main" java.lang.NoSuchMethodError: C1.test()V
at C2.main(C2.java:6)
Java Result: 1
P.S. I'm using Netbeans.
This means that you are running your class C2 with an old version of class C1 in the classpath (a version that did not yet have the test() method).
Make sure you don't have old versions of C1.class somewhere. Remove all your *.class files and recompile everything, and then try to run it again.
Addition: As Kevin Bowersox noted in a comment, your main method must look like this:
public static void main(String[] args)
It must take a String[] as an argument.
It will properly compile and run only if main function will have String tab as args.
But also check versions of class C1 and C2, try rebuild your project to recompile that classes.
public static void main(String args[]) {
C1.test();
}
i think you should import it as
import p1.*;
Than you will get access to all classes and member functions in it.
Netbeans sometimes likes to get stuck after some changes and clean build doesn't work then.
Try editing each file that has been recently modified and saving it again (e.g. put a whitespace in a random place). After that, clean and build the project again.
If my memory refreshes and as Jesper pointed out, I also encountered that same issue NoSuchMethodFoundException under that same scenario (having still old class references that have not been cleaned).
I just copied your code snippets with 2 different packages directly in to my netbean, compiled and runned C2. It did print the BOOM! message.
In my case using :
public static void main(String args[]){
}
does not make a difference when I compiled and runned the code.
public static void main(String params[]){
}
It makes sense since the main class should have the correct method signature of main.
Here args or params, should not make a huge difference, I believe; as what we have inside the method is simply a reference for the inner body of the method that it uses.
Still definitely it is good practice to follow the standard signature for main.
I would recommend to clean the project and copy the contents from scratch in a new project and build it again, sometimes netbeans can go crazy.
So, my code is pretty simple. Just wanted to try and create a package.
// /home/user1/Code/packageTest/src/myPackage/Test.java
package myPackage;
public class Test
{
public static void main(String[] args)
{
System.out.println("Hello, world.");
}
}
let's say this code is in some directory myPackage.
If I comment out the first line (package specification) the code runs fine, prints the message. It compiles either way, but if it compiled with the package line not commented out, it causes a run-time error.
What do I need to do to successfully make a package? I can't seem to find the right search terms to turn up a real explanation on this, only stuff about package naming conventions, why they're used to separate namespaces, bla bla bla. The next part of this experiment was going to be trying import my own packages, obviously I didn't even get that far.
Something to do with the classpath perhaps...? What and where is my "base directory"? Any information on this would be greatly appreciated.
You should create the class in a java file in a directory with name myPackage. Then you should come out of that directory and compile it as follows
javac myPackage/Test.java
Then run it with fully-qualified-class-name (FQCN) as follows:
java myPackage.Test
E.g.
C:\Temp\test1>type myPackage\Test.java
package myPackage;
public class Test
{
public static void main(String[] args)
{
System.out.println("Hello, world.");
}
}
C:\Temp\test1>javac myPackage/Test.java
C:\Temp\test1>java myPackage.Test
Hello, world.
C:\Temp\test1>
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'.