Error: Main Method Not Found in Class Please Define the Main Method As: Public Static Void Main(String[] Args)

Why can not pass int as argument in main method

The error tell evething. either you forget to declare main method or somthing missing in main method declaration. In Java Programming The main() method is a special method that serves as the externally exposed entrance point by which a Java program can be run. To compile a Java program, you doesn't really need a main() method in your program. But, while execution JVM searches for the main() method and starts executing from it.

You declare int argument in main method but it shoud be String[] args. now one question arise in your mind Why String args[]? because When you run a Java program from a command line, you are allowed to pass data into that program as comnand line arguments.Your Java program will run from a main() method, and the arguments you typed on the command line will appear to the Java program as Strings in that array.

Here down is right syntex:

public static void main(String[] args) {
// Some code inside main function....
}

And your code should be:

public class Main {
static void printSomeNumber()
{
int a = 10;
int b = 20;
System.out.printf("%d %d", a, b);
}
public static void main(String args[]) {
printSomeNumber();
}
}

Error :: Main method not found in class

To start a java program you need the main method which not define in your code you can make it like this :

public class Test {

public void printPhoto(int width, int height, boolean inColor) {
System.out.println("Width = " + width + " cm");
System.out.println("Height = " + height + " cm");
if (inColor) {
System.out.println("Print is Full color.");
} else {
System.out.println("Print is black and white.");
}
// printPhoto(10, 20, false); // Avoid a Stack Overflow due the recursive call
}

//main class
public static void main(String[] args) {
Test tst = new Test();//create a new instance of your class
tst.printPhoto(0, 0, true);//call your method with some values
}

}

Error: Main method not found in class singleInheritance.A, please define the main method as: public static void main(String[] args) or a JavaFX

I think you have wrong params of running the app. Check the config and look at the Main class in it. Check that it points to singleInheritance.B rather than singleInheritance.A In IDEA it looks like that:
enter image description here

Error: main method not found in class please define the main method as: public static void main(String[] args)

I am not sure how you are executing it.

I have saved the above code as example.java in my machine and used the following commands in command prompt

For Compiling: javac example.java

For Executing: java example

Note that the main method is in example class so you have run the example class not the Box class

Edit:
Technically any java code can be compiled but for running a standalone application, the main method is needed. Even when you save the file as Box.java and compile it, there will be no compilation errors. But for running JVM needs main method. so you have to run example class not the box class

How to delete column with shiftColumns method

Yes, here are still problems in Sheet.shiftColumns even in latest version Apache POI 5.1.0. The shiftColumns does not removing the cells properly when negative shifted. That's why the error while writing then.

If you explicitly remove the cells of the column you want over-shfting, then the error is gone. So we need to remove all cells from second column (index 1) before shifting third column (index 2) to left.

But there are additional problems too. Even the last version does not update the calculation chain while shifting formulas. This is the problem of this Q/A: shiftColumn method is not working when cell has formula.

Complete example:

import java.io.FileInputStream;
import java.io.FileOutputStream;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xssf.model.CalculationChain;
import org.apache.poi.ooxml.POIXMLDocumentPart;
import java.lang.reflect.Method;

public class ExcelShiftColums {

private static void removeCalcChain(XSSFWorkbook workbook) throws Exception {
CalculationChain calcchain = workbook.getCalculationChain();
Method removeRelation = POIXMLDocumentPart.class.getDeclaredMethod("removeRelation", POIXMLDocumentPart.class);
removeRelation.setAccessible(true);
removeRelation.invoke(workbook, calcchain);
}


private static void removeColumn(Sheet sheet, int column) {
for (Row row : sheet) {
Cell cell = row.getCell(column);
if (cell != null) {
row.removeCell(cell);
}
}
}

private static int getLastFilledColumn(Sheet sheet) {
int result = 0;
for (Row row : sheet) {
if (row.getLastCellNum() > result) result = row.getLastCellNum();
}
return result;
}

public static void main(String[] args) throws Exception {

String inFilePath = "./ExcelExampleIn.xlsx"; String outFilePath = "./ExcelExampleOut.xlsx";

//String inFilePath = "./ExcelExampleIn.xls"; String outFilePath = "./ExcelExampleOut.xls";


try (Workbook workbook = WorkbookFactory.create(new FileInputStream(inFilePath));
FileOutputStream out = new FileOutputStream(outFilePath ) ) {

Sheet sheet = workbook.getSheetAt(0);

int lastFilledColumn = getLastFilledColumn(sheet);

removeColumn(sheet, 1);
sheet.shiftColumns(2, lastFilledColumn, -1);

if (workbook instanceof XSSFWorkbook) removeCalcChain((XSSFWorkbook)workbook);

workbook.write(out);
}
}
}


Related Topics



Leave a reply



Submit