I have just started learning Java using an online course.
When I create a new project with IntelliJ, I don't have the Main class in the src folder. However, the instructor is saying IntelliJ should create the class "Main" by default.
HOW I SHOULD HAVE IT:
-"Name of the project"
-Idea
-src
-"com.myname"
-Main (the Class)
HOW I HAVE IT:
-"Name of the project"
-Idea
-src (which contains nothing)
Therefore, I have to create manually the class "Main" by clicking on "src" and creating a class named "Main" with this code inside:
Public class Main {
public static void main(String[] args) {
}
}
MY QUESTION: how can I set IntelliJ to automatically create the main class every-time a new create a new project?
Many thanks for your help.
Create the project from template:
Command Line App will add a class with the empty main method.
Related
I want to create a single jar file from multiple packages. I have created the jar using below command but when I'm importing it to a project as a dependency it is not working.
jar cfe output/jar/my-java.jar Main src/pkg1/pkg0/*.class src/pkg1/*.class src/pkg2/*.class
My project structure is something like below structure
src
pkg1
A.java
B.JAVA
pkg0
E.java
pkg2
C.java
D.java
My Example code is something like
import pkg1.A;
public class Main {
public static void main(String[] args) {
A.printMe("Hello World");
}
}
error that I'm getting is:
java pkg1 not exist
But in the editor(IntelliJ), it is not showing errors and also i'm able to import class but not package.
import pkg1: showing red means error in the editor
import pkg1.A: not showing red means no error in the editor
Note: I don't want to use maven.
An unzip -t something.jar shows the actual file structure of the jar file (zip). It is the same as the class structure of it (except that instead "/", a "." is the separator).
In your case, the problem will be that src will be on the top level, and not pkg1. Either import src.pkg1 (very dirty), or play a little bit more with the directories / jar flags.
just call your method it automatic get path if you correctly put your jar in your current project.`project 1
pkg com.test.demo
class test{
public static void m1(){
System.out.println("project 1 in method 1 );
}
}
in project 2 put jar of project 1
pkg com.test.demo
class Test1{
public static void main(String...){
System.out.println(test.m1())
}
}
IntelliJ does not find a main class in my Java application project. The project was cloned from a git repository so had no run configuration. I go to Edit Configurations, add a new Application template, go to Main class: and it says "No matches found in project".
So, manually searching through the hierarchy I find the .java file that contains the main function but it will not accept it as the main class. I've pasted the file below to prove that it has the correct main function.
public class AdvanceWarsGameHandler implements IGame
{
private Image mImage;
private String mTitle;
public AdvanceWarsGameHandler()
{
mTitle = "Advance Wars Game";
mImage = new Image("/OffBrandCerealOopsAllCarries2-01.png");
}
//Game logic unrelated to graphics goes here
#Override
public void update(Game game, float deltaTime)
{
}
//Update, but for graphics
#Override
public void render(Game game, Renderer renderer)
{
renderer.drawImage(mImage, game.getInput().getMouseX(), game.getInput().getMouseY());
}
public static void main(final String args[])
{
//Creating and starting an instance of AdvanceWarsGameHandler
AdvanceWarsGameHandler advancewars = new AdvanceWarsGameHandler();
Game myGame = new Game(advancewars);
myGame.start();
}
public String getTitle()
{
return mTitle;
}
}
So the question is, why is the IntelliJ project not recognizing the main function in this file, or what is IntelliJ looking for as the "Main class" of an application?
Okay, hopefully this answer will help others who are unfamiliar with IntelliJ IDEA.
The solution came in two parts
Part 1: Missing compilation directory.
Since I did not create the project from new and instead I cloned a Git repository, there was no default compilation directory set up.
To access this in IntelliJ IDEA go to File -> Project Structure -> Project and set the "Project compiler output" so the project can actually compile.
Part 2: Setting up the modules
The original project was created in Eclipse which has packages. In order to get those packages to work in IntelliJ, I had to go to the Modules tab of the Project Structure menu and set my src and res folders as Source and Resource Folders. This allowed IntelliJ to find the main() function in my class and the program ran as expected.
This solved my problem though if any of you IntelliJ users out there can see anything bad about what I did to get it working, please comment.
I am learning how to use packages in Java, but I am running into trouble when trying to implement them. I have a simple class called Main which appears as follows:
public class Main
{
public static void main(String[]args)
{
System.out.println("Package Test...");
}
}
The directory of this class is: C:\Users\MyComputer\Desktop\Packages\Main.java
When I compile this class, I run into no trouble. However, when I add "package com.example.mypackage;" to the top of the .java file, compile the program, and try to run the program, I receive the following error: "Error: Could not find or load main class Main"
What can I do to solve this problem?
If the path of your class is C:\Users\MyComputer\Desktop\Packages\Main.java then your class is not in a package. In this instance "Packages" is your project folder, and it only contains the one java class.
If you want package com.example.mypackage; to work, then your path needs to be:
C:\Users\MyComputer\Desktop\Packages\com\example\mypackage\Main.java
I have an Interface class checked out from an svn project, and created an Interface_Tester class to preview the interface. However, when I try to run it in Eclipse I get the following error:
Error: Could not find or load main class Interface_Tester
Here is the code from the interface_tester class -
public class Interface_Tester {
public static void main(String[] args){
Interface test = new Interface();
}
}
I have seen a similar question asked on here but did not understand the answer. I need a step-by-step guide to the solution as I am a beginner. Thank you
UPDATE:
I tried running the Interface class itself and had the same error... All classes are being shown in the project explorer on the left so why is eclipse unable to locate any of them?
On the left side of Eclipse you have a project view. Try right-clicking your Interface_Tester from there and in a dropdown choose run. May be you you are trying to run something else
When I want to run my java code, I have this problem :
Exeption in thread "main" java.lang.ArrayIndexOutOfBoundsException:0
at com.ibm.icu.text.BreakDictionary.main(BreakDictionary.java:44)
i can't run my code... It's simple
public class main{
public static void main(String[] args){
System.out.print("Hi");
}
}
WTF is com.ibm.icu.text.BreakDictionary?
Make sure of the class whose main you are running. I suspect you're not running your class. Your java command should look like this:
java -cp somepath com.mycompany.mypackage.MyClass
Better yet, use an IDE like eclipse that make it easy to run a class
Also, rename your class from main to MyClass - anything with a leading capital letter - it's good coding style
Exeption in thread "main" java.lang.ArrayIndexOutOfBoundsException:0
at com.ibm.icu.text.BreakDictionary.main(BreakDictionary.java:44)
This exception says that you are running the main method in a class named BreakDictionary. In your sample code the class is called main.
You can solve this in eclipse by right-clicking your class and select run as and then java application. This will ensure that you are using your main class as the entry point and not BreakDictionary (which seems to either be on your classpath or you are simply not running the correct project in eclipse).