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.
Is it possible to change the name of a class retrieved using:Foo.class.getName() (or getSimpleName() or getCanonicalName()).
I know that those methods are part of java.lang.Class<T> the question itself is if there is a way to tell java.lang.Class<T> what is the name that I want it to display for my class.
I know that it "opens the door to tricky things" since that name is used by the reflection libraries to do "stuff" and bla bla bla. nevertheless I was wondering wether is posible to call Foo.class.getSimpleName() and get something like MyFoo.
All of this of course without string manipulation which is the last alternative I have.
Find the src.zip in your JDK. Extract java/lang/Class.java into some directory and modify getSimpleName() method. For example like this:
public String getSimpleName() {
return "MyName"; // return MyName for any class
}
Compile it normally with javac (you will get many warnings, ignore them). Remove all additional classes created like Class$1.class, leaving only java/lang/Class.class file. Put it into jar:
$ jar -c java >myclass.jar
Now prepend the bootstrap path with your new jar. For example, let's consider this test class:
public class Test {
public static void main(String[] args) {
System.out.println(Test.class.getSimpleName());
}
}
$ java Test
Test
$ java -Xbootclasspath/p:myclass.jar Test
MyName
I don't even want to explain how dangerous it is. Also according to the Oracle binary code license (supplemental term F) you cannot deploy your application this way.
You may try Powermock which according to their home page allows mocking final classes although it needs to use it's own class loader.
Other mocking frameworks that do not do byte code manipulation with custom class loaders such as Mockito and Easymock cannot work with classes that are final which java.lang.Class is.
I started to learn Java couple days ago. And I have this burning question. Is empty .java file name a valid source file name?
.java
Yes, save your java file by .java then compile it by javac .java and run by java yourclassname
class X {
public static void main(String args[]) {
System.out.println("Hello World");
}
}
to compile - javac .java
to execute - java X
Yes it's working because java compiler doesn't consider it saves file name or not except our class having public specified we can save any name or empty but when ever trying to execute we must use our class name because, jvm creates byte code ourclassname.class so we using
java className
Yes Empty .java file name works, but class must not be public, it means that it must be default.
If class is public then following error occour:
D:\Testjavac>javac .java
.java:1: error: class Empty is public, should be declared in a file named Empty.
java
public class Empty
^
1 error
Yes You can have .java file withought nay name . you have to compile it by javac .java(it compile successfuly) and run it by java clasnname.(so you must provide a class name)
Yes, but don't do this often.
You can't create any classes in that file that are public or private, so any class that made use of any class defined here would have to be in the same package.
at anytime you can have only one public class in the file and if you use public class then that class name should be the file name.
It's a simple class and I am a beginner with Java.
I don't know why this code is not running and why it gives an error :
Could not find or load main class
class tuto{
public static void main(String[] args){
System.out.println("Hello World");
}
}
There are a couple things which jump out at me when I look at your question.
The first thing is that you have unresolved compiler errors. If you see that red 'x' on the Problems tab, you should fix all the errors there before trying to run anything.
The second thing is that your class name doesn't match the file name in which it is defined. For public classes the name of the class and the name of the file must match, and while your class isn't public, this is a widely followed Java convention and you will confuse people if you don't follow it.
As to your actual question, my best guess is that you have placed your class into a package and not declared it as such in your source code. If you go look at the Problems tab, it will tell you what is wrong and (often) how to fix it.
I can approximate your error message if I do the following:
In this case, I have an error over in the Problems tab complaining about the declared package.
Check to see if you have something similar:
If you do, you can right-click the error message and select "Quick Fix", and eclipse will pop up a dialog offering to add the package declaration for you:
In your code there is a compile error, that is because Syteme change it to System
Syteme.out.println("Hello World");
should be
System.out.println("Hello World");
P.S
And in Java when you have a public class in a file, then file name must be that class name. It is a must. Otherwise you will get an error.
If you have this class in a package then you must specify the package declaration first
e.g
package abc;
System.out.println not Syteme.out.println.
In Java (as somebody has already pointed) the name of the file should be of the same name of the main class within the same file.
Moreover, you should also declare an array using this syntax array_type [] array_id and not array_type array_id [].
There might be a couple of problems:
If the class is in a package, make sure you specify it. eg: package com.pak;
The class with main method always needs to be public. public class apples{}
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.