Intellij14.1 Error:No main class for module - java

I have just try HelloWorld
public class hw {
public static void main(String []args){
System.out.println("HelloWorld");
}
}
but, console said when I tried to compile:
Error:No main class for module: HelloWorldTest
Error:Compilation failed
I don't know I don't know what I have wrong, Why it give me this warn ?

Create a file HelloWorld.java at the root of your source package:
public class HelloWorld {
public static void main(String... args) {
System.out.println("HelloWorld");
}
}
Then from the project explorer (left pane), right click on your HelloWorld class, and select Run 'HelloWorld.main()'

Do you have the Haxe plugin installed?
If so it's likely the source of the problem.
https://devnet.jetbrains.com/thread/435604?tstart=0 and https://devnet.jetbrains.com/thread/435708
Try the fix in the first link or uninstall the plugin.

Related

Debug Imported Files in VS Code

I have recently set up the Java extension for VS Code.I have a folder that contains two files Main.java and Person.java. Main.java calls Person.java. When I set a breakpoint in Main.java, everything works as normal.However, when I set a breakpoint in Person.java, it just skips over it.
Are there any workarounds in this issue?
Main.java file
public class Main {
public static void main(String[] args) {
System.out.println("Hello World!");
Person person = new Person();
person.speak();
}
}
Person.java file
public class Person {
public void speak(){
System.out.println("Speak!");
//breakpoint on this line right here gets skipped
System.out.println("Speak!");
}
}
Run -> Start Debugging, it stops at the breakpoint:

Custom library gives "no package found"

I decided to make a custom .jar library just for the hell of it.
It's insanely basic, it's package name is "add" and looks like this:
public class addtogether{
public void addtogether(int a, int b, int c){
c=a+b
}}
So I try importing it in a file, by doing this:
public class test{
import add.*;
public static void main (String[] args){
int x;
add(4,5,x);
System.out.println(x);
}
}
and surprise surprise! No package found.
I did the whole dance of adding it to the IDE in the preferences section, but it still doesn't work.
help please.
A very naive step by step implementation for using a custom jar:
Create a java project with a utility class containing your functionality. (You can use the class defined above.) Example: (Project Name: ArithmeticUtility, Jar Name : ArithmeticUtility.jar, Package name: com.arithmetic, ClassName: Calculator)
Export it as a jar.
Create another java project add the jar by right clicking on the project-> Build Path-> Configure Build Path->Add External jars-> Apply-> Ok.
Use the class in your present project by mentioning the required imports. (Example: import com.arithmetic.*)
Please edit your code as below:
import add.*; // import statements should be at top after package outside
public class test{
public static void main (String[] args){
int x;
addTogether testObj=new addTogether();
int result=testObj.add(4,5);
System.out.println("Result: "+result);
}}
public class AddTogether{
public void addtogether(int a, int b){
return a+b; // your method has no return statement
}}
Hope it helps.

How to call function in another project in Java

I checked few other stackoverflow posts. But I am not able to call a function that is present in another project.
SampleTwo.java
package a.two;
public class SampleTwo {
public static void bar() {
System.out.println("Bar");
}
public static void main(String[] args) {
bar();
}
}
Updated SampleOne.java
package a.samp;
import a.two.*;
public class SampleOne {
public static void foo() {
System.out.println("Foo");
SampleTwo.bar(); // <------ I want this to work
}
public static void main(String[] args) {
foo();
}
}
Here is my project properties of both projects
[Version 1: Before blackjack26's answer]
Eclipse shows the error SampleTwo cannot be resolved
Can you please tell if I am missing something? Thanks!
[Version 2: After blackjack26's answer]
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
SampleTwo cannot be resolved
[Version 3] Removed SampleOne from SampleTwo's project property fixed it.
In your SampleOne Java file, you are missing and import to access SampleTwo.
You should add the following to your imports for SampleOne:
import a.two.SampleTwo;

Using package while running; Error "Could not find or load main class"

classpath= C:\Program Files\Java\jdk1.8.0_111\jre\lib\;C:\Program Files\Java\jdk1.8.0_111\bin;
path=C:\Program Files\Java\jdk1.8.0_111\bin;C:\Program Files\Java\jre1.8.0_111\bin;
package seleniumTest;
public class SampleTest {
public static void main(String[] args)
{
System.out.println("hello");
}
}
My question is I am not able run the Java file while using the package.
I am new to Java so not able to find the root cause. Please help me out.
Its not able to reach the main class.

How to call a paint class from another .jar file

I have made a .jar file in Eclipse to call a paint class. When I finish it gives me an error:
JAR export finished with warnings. See details for additional information.
Exported with compile warnings: Graphics/src/G1.java
Jar export finished with problems. See details for additional information.
Could not find main method from given launch configuration.
Here is my code to call the paint method:
public class G1Starter {
public void main(String[] args)
{
Graphics1 g1 = new Graphics1();
g1.repaint();
}
}
I tried making a main method in the Graphics1 class but it did not work.
Add the static keyword so that the application has a valid entry point
public static void main(String[] args)
Method 'main' should be 'static'
public static void main(String[] args)

Categories