Just for my learning about -Xbootclasspath, I compiled a custom String class (modified the toString method to become "return "Hey Sugar,: " + this;") and kept it in C:/test/test2 and then create a simple Java program and used String as below:
String str = new String("sss");
System.out.println("############# " + str.toString());
Now, I compiled my test class and then ran it as java -Xbootclasspath/p:test2/ MyTest but I do not see the effect.
So, I am assuming my modified version of String class is not being loaded.
Am I doing something wrong?
Also, if I compile the keep the .class of modified String class in same folder as my test Java class then I get below error while trying to compile my test Java class.
MyTest.java:37: error: cannot access String
public static void main(String[] args) {
^
bad class file: .\String.class
class file contains wrong class: java.lang.String
Please remove or make sure it appears in the correct subdirectory of the classpath.
Related
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.
If i compile any java program, I got this error
b1.java:3: cannot access String
bad class file: .\String.java
file does not contain class String
Please remove or make sure it appears in the correct subdirectory of the classpath.
public static void main(String ar[])throws IOException{
As visible from your compilation step in the terminal,
String is already a Java-library class available in JDK.
Hence,you can't give such name to your program/class.
Hence,you receive such error.
Try naming it something else like public class MyString and rename your source code as MyString.java---recompile it and then you won't receive such errors.
String is a datatype defined within the Java-Library. You cannot name a variable as String or any other name of datatypes, in that way you cannot name it for a class.
String is in-built Class in Java. Hence you can not give the name for Class as String. you can check it by - just give the name for Class as Class string and save it as string.java . now it will compile and run properly. but according to naming convention first letter of Class Name must be in UpperCase , so you can name it as MyString.java
I am trying to write a program, but I'm getting this compiler error:
Main.java:1: error: class WeatherArray is public, should be declared in a file named WeatherArray.java
public class WeatherArray {
^
1 error
I have checked my file names, and my public class is the same as my .java file.
How can I fix this?
Here is my code:
public class WeatherArray {
public static void main(String[] args) {
// ...
}
}
Name of public class must match the name of .java file in which it is placed (like public class Foo{} must be placed in Foo.java file). So either:
rename your file from Main.java to WeatherArray.java
rename the class from public class WeatherArray { to public class Main {
The name of the public class within a file has to be the same as the name of that file.
So if your file declares class WeatherArray, it needs to be named WeatherArray.java
This happens when you have 1 name for the Java class on hard disk and another name of Java class in the code!!
For example, I renamed my MainActivity class to MainnActivity only (!) in the code. I got this error immediately.
There is also a visual indicator in the Project tab of Android Studio - a class inside a class, like you have nested classed, but with an error indicator.
The solution is to simply rename class name in the Project tab (SHIFT + F6) to match the name in the Java code.
I had the same problem but solved it when I realized that I didn't compile it with the correct casing. You may have been doing
javac Weatherarray.java
when it should have been
javac WeatherArray.java
You named your file as Main.java. name your file as WeatherArray.java and compile.
your file is named Main.java where it should be
WeatherArray.java
Yes! When you face these type of problem then try to following points
Check Your .java file name and class name.
If Class name and public class name are not the same then RENAME class name.
I my case, I was using syncthing. It created a duplicate that I was not aware of and my compilation was failing.
To avoid this error you should follow the following steps:
1) You should make a new java class
2) Name that class
3) And a new java class is made
I encountered the same error once. It was really funny. I had created a backup of the .java file with different filename but the same class name. And kept on trying to build it till I checked all the files in my folder.
In my case (using IntelliJ) I copy and pasted and renamed the workspace, and I am still using the old path to compile the new project.
In this case this particular error will happen too, if you have the same error you can check if you have done the similar things.
The terminal is not case sensitive when writing "Javac [x].java", so make sure what you write in the terminal matches the filename and class name.
My class name and file name were both "MainClass", but I was compiling using "Mainclass". Notice I forgot to make the "c" capital.
From Ubuntu command line:
//WeatherArray.java
public class WeatherArray {
public static void main(String[] args) {
System.out.println("....Hello World");
}}
ls
WeatherArray.java
javac WeatherArray.java
ls
WeatherArray.java WeatherArray.class
java WeatherArray
....Hello World
Of course if you name your java file with different name than WeatherArray, you need to take out public and it would be:
// Sunny.java
class WeatherArray {
public static void main(String[] args) {
System.out.println("....Hello World"); }}
// javac Sunny.java; java WeatherArray
If you make WeatherArray class from public to default.
public class WeatherArray =====> class WeatherArray
then you do not get any error and
you can easily compile your code by just writing
==> javac any_name_you_assign_to_file.java
Now a WeatherArray.class will generate.
To run your code you have to use class name
==> java WeatherArray
Compile WeatherArray.java instead of Main.java
This error comes if you have not saved your source code with the same name of your public class name.
If your class name is the same as the filename then check that it does not contain any zero width character
I accidentally copied a class name with invisible whitespace which caused this exception
Eclipse was able to build the file and Gradle was not
This can be very confusing
The answer is quite simple. It lies in your admin rights. before compiling your java code you need to open the command prompt with run as administrator. then compile your code. no need to change anything in your code. the name of the class need to be the same as the name of the java file.. that's it!!
error example:
public class MaainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the view from activity_main.xml
setContentView(R.layout.activity_main);
}
}
correct example:just make sure that you written correct name of activity that is"main activity"
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the view from activity_main.xml
setContentView(R.layout.activity_main);
}
}
when you named your file WeatherArray.java,maybe you have another file on hard disk ,so you can rename WeatherArray.java as ReWeatherArray.java, then rename ReWeatherArray.java as WeatherArray.java. it will be ok.
My file name is Temp.java and inside it I have this. I'm using eclipse IDE
/*package*/ class Test {
public static void main(String args[]) {
System.out.println("test");
}
}
So I was unable to run this as java application. I change my class name to Temp
class Temp {
....
}
Now I can. Can someone explain me why ?
This is probably a limitation of Eclipse. The code runs well from command line.
As I understand, you are trying to embed your unit tests in the same file with the class under test. This is a nice idea and I totally concur with it. You can read more about how you can succeed in Ben J. Christensen's blog post. Generally, he suggests placing the tests in a static inner class, not a standalone class in the same file.
An example from the Netflix Hystrix framework: HystrixCircuitBreaker.UnitTest
The code below, located in Temp.java, compiles and runs fine with Netbeans:
class Whatever {
public static void main(String[] args) {
System.out.println("hello");
}
}
The problem is with eclipse, i think you are trying to run using right click -> run as -> Java Application, unfortunately eclipse is not showing this option if the class is not public.
But you can still run the class using Alt+Shift+X,J.
Its not the problem with Java, its with Eclipse.
The name of the file should be the same as the class name which is public and has the main() method. In your first case the file name Temp.java will compile and will create Test.class file not Temp.class because there is no Temp class declared in your file.
after .class file is created , run it with java Test
so here's an example
//Filename abc.java
public class hi
{
public static void main(String[] args)
{
System.out.println("Hell");
}
}
the output
abc.java:1: class hi is public, should be declared in a file named hi.java
public class hi
^
1 error
but if you do this
//Filename abc.java
class hi
{
public static void main(String[] args)
{
System.out.println("Hell");
}
}
it will create hi.class file so
D:\>java hi
Hell
The class (which main should be run) inside the .java file must have the same name as the file. If the class is not public (as in your case) the class will compile but it can't be run since Eclipse tries to load the class according to the file name.
I think this is a situation every Java programmer runs into if they do it long enough. You're doing some debugging and make a change to class. When you go to re-run the program, these changes don't seem to be picked up but rather the old class still seems to be running. You clean and rebuild everything, same issue. Sometimes, this can come down to a classpath issue, of the same class being on the classpath more than once, but there doesn't seem to be an easy way to figure out where the class being loaded is coming from...
Is there any way to find the file path for the class that was loaded? Preferable something that would work either if the class was loaded from a .class file or a .jar file. Any Ideas?
Simply run java using the standard command line switch"-verbose:class" (see the java documentation). This will print out each time a class is loaded and tell you where it's loaded from.
If you wanted to do it programmatically from inside the application, try:
URL loc = MyClass.class.getProtectionDomain().getCodeSource().getLocation();
(Note, getCodeSource() may return null, so don't actually do this all in one line :) )
public static URL getClassURL(Class klass) {
String name = klass.getName();
name = "/" + convertClassToPath(name);
URL url = klass.getResource(name);
return url;
}
public static String convertClassToPath(String className) {
String path = className.replaceAll("\\.", "/") + ".class";
return path;
}
Just stick this somewhere, and pass it the Class object for the class you want to find the definition of. It should work regardless of where it called from, since it calls getResource() on the class being searched for.
public static void main(String[] args) {
System.out.println(getClassURL(String.class));
}
Sample output:
jar:file:/System/Library/Frameworks/JavaVM.framework/Versions/1.6.0/Classes/classes.jar!/java/lang/String.class
As the class needs to come from somewhere in the class path, I would recommend to simply print the class path and check if there's an older version of you class somewhere.