I'm learning java and practicing on sololearn.com and I copied one of the examples to practice typing code. However the code here -
//Create myClass
class Loader extends Thread {
public static void main(String[] args) {
}
public void run(){
System.out.println("Hello Young World");
}
}
public class MyClass {
public static void main(String[] args) {
Loader obj = new Loader();
obj.start();
}
}
isn't printing "Hello Young World" to my console. In fact I had to add a 'main()' method to the Loader class just to run MyClass.java. However in the example their code ran without having to include a main method in Loader. Maybe they have customized their environment to allow for this type coding and IntelliJ just has different rules. Please could someone copy the code on their machines and run it with IntelliJ to see if they run into the same problem?
I've troubleshooted, but the code seems to be solid.
I guess you start the wrong main-method (the main from the Loader class which is empty). As you noted, you donĀ“t have to add a main-method in your Loader class. Please remove the method and start the main-method from MyClass.
If you have several main-methods, you can choose which one should be executed in the run configurations. In your example, it should look like this:
In this case, make sure you select the one you like to execute.
Another way to execute the right main-method, is to select the class which contains the main-method and hit the play button on the left side:
To run the program in my console I had to 'java MyClass' rather than running 'java MyClass.java' which executed the whole file rather than just the class whose main method I wanted to call!
To run the program in IntelliJ I had to make sure that the right Class was being called as pointed out above.
Related
SOLVED, Program was in location with national symbol in it's path.
I just started studying java, but every program i try to start (even example ones from my course) shows an error.
Error: Could not find or load main class "Any class name of program I try start"
C:\Users\Mine\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds)
edit:
example of code, but happens to any code.
public class Hello {
static void hello(){
System.out.println("Hello, World!");
}
public static void main(String[] args) {
hello();
}
}
This error means that when Netbeans is invoking the JVM, the JVM cannot find the class file for the class Netbeans is telling it to run. When you create a project in Netbeans, the classpath will be configured for you by the IDE, so you shouldn't normally see this error unless you have deleted the auto-generated main class and made a new one from scratch in the wrong place.
So the first thing to do is check what class Netbeans is using as the main class:
Right-click on the project name in the Projects tab and click on "Properties"
Then click on "Run" and check the name of the class in "Main Class":
Note in my example the class is called "tests.Test". This means the class Test in the package "tests". In your question, your class "Hello" doesn't have a package declaration at the top (although you may have chosen not to copy this). If you have no package (and you really should be using packages, even for trivial programs like "Hello, World!", just to get used to doing so, if nothing else), the "Main Class" entry should just be the class name.
So you need to either move your class into the package specified in this parameter, or change this parameter to match the fully qualified name of your main class
Error: Could not find or load main class "Any class name of program I try start"
C:\Users\Mine\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds)
You are attempting to run a class called Any class name of program I try start, however the name of your class is Hello.
I don't know how Netbeans does things, but I would first try compiling and running the program without netbeans.
javac Hello.java
java Hello
If that works then open up the run settings in netbeans and make sure that it is doing the same thing.
Just make a new main class or just re-type public static void main(String[] args) { } and that's it.
When i'm trying to run in Eclipse program which has no main method as Java Application it always requires a main method to define. Could anybody please explain is there any simpler way to make configuration run without implementing
public static void main(String[] args) {
}
Every program requires a main method.
The public static void main(String args[]) starts the main thread which your program will run on.
There's more information available here.
You need a main method to run your program.
public static void main(String[] args){
YourClass newObject = new YourClass();
}
Put this method in your class (replace YourClass with your existing class) and hit the run button.
run in Eclipse program which has no main method
NO.You won't be able to run a class by using IDE like Eclipse or anything else. Even you won't be able to run your java program by using Java command from your command prompt, until your java program has public static void main(String args[]) method on it. If you try to run, you will get below mentioned error
Error: Main method not found in class Test, please define the main
method as: public static void main(String[] args)
But you should also know that, we can run a Java program without main method on managed environment like the case of Applet, Servlet and MIDlet. There are actually different types of execution model available in Java, for example Applets which run on browser doesn't have main method. Servlet is also java program, which runs in a Servlet container and since Servlet is also a Java program, we can say that it runs without main method. Third one on this category is MIDlet, which runs on mobile devices.
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.
import stanford.karel.*;
public class MidpointFindingKarel extends SuperKarel
{
public void run()
{
move();
}
}
This is my example code.the method run() is inherited from the SuperKarel class.
All i want is to run this code and codes like this as a java application.but when i click on the 'Run as', i see only 'run as java applet' there.how to get the option of 'run as java application there'.i know how to create a run configuration but when i do,that configuration is being applicable only to a particular java file.how to run codes like these without creating a run configuration everytime for a new java file??
You need to declare main() as
public static void main(String[] args)
For instance you might change your class to
public class MidpointFindingKarel extends SuperKarel {
public void run() {
move();
}
public static void main(String[] args) {
MidpointFindingKarel mfk = new MidpointFindingKarel();
mfk.run();
}
In order to do that, you need a
public static void main(String[] args) {
}
method in your class. It may be that MidpointFindingKarel is used by the main class which is defined somewhere else. In that case, search for it and run that class.
In essence, Eclipse populates the run menu based on the current class that has focus in the editor.
Once, you have the run configuration set up, you can of course simply right click on the run menu dropdown box and select the class to execute (typically, top-left on the toolbar).
You need to add a main method in your class.
Run as Java Application is applicable only to classes having public static main method. Change the code to add main method. You will find the option of run as.
I've been running different individual Java .java files in the Netbeans IDE by right-clicking the .java files themselves in the Project Explorer of Netbeans (the portion normally at the upper left part of Netbeans).
However, i've been googling on how to make a class file run another class file using code, but to no avail.
I have a project named "loadanotherfile" with 2 files, namely: Loadanotherfile.java and otherfile.java
I'm trying to make Loadanotherfile.java run otherfile.java, but I'm not exactly sure how. I read about Classloaders and URLClassloaders however these methods don't seem suitable for my purpose of running another .java file.
Below is the code of the 2 files i mentioned.
Loadanotherfile.java
package loadanotherfile;
public class Loadanotherfile {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
System.out.println("Hello World!");
// TODO code application logic here
}
}
otherfile.java
package loadanotherfile;
public class otherfile {
public static void main(String args[])
{
System.out.println("This is the other file.");
}
}
I have a feeling that the task has something to do with using the "import" syntax (namely something like import loadanotherfile.* but even if my guess is correct, I'm still not sure on how to make my Loadanotherfile.java run otherfile.java using code.
How can I load otherfile.java using Loadanothefile.java?
Cheers
In Loadanotherfile.java
otherfile.main(args);
Compile the two together, and then from Loadanotherfile,
otherfile.main(args);
will do the trick. You don't need to import since you're in the same package. Note the linked tutorial.
I would investigate (however) class instantiation, and creating an instance of a new class to invoke upon. Invoking static methods from static methods isn't very OO.
Try This:
className.main(Args){
}
This works! ive tested it myself.
Check the public void main line. If there IOException and not there then insert
in Loadanotherfile.java
use this
otherfile.main(args);{
}