Error: Could not find or load main class ExcelReadWrite - java

I have a class ExcelReadWrite that runs fine when I run it through eclipse.But when I try to run it from command line I need to set classpath of dependent classes.
set CLASSPATH=C:\Users\NICSI\.m2\repository\org\apache\poi\poi-ooxml\3.11\poi-ooxml-3.11.jar;C:\Users\NICSI\.m2\repository\org\apache\poi\poi\3.11\poi-3.11.jar;
After setting class path it compiles successfully but when I execute my class then this exception occurs.
Error: Could not find or load main class ExcelReadWrite
public class ExcelReadWrite {
public static void main(String[] args) {
String inputFile=args[0];
String outputFile=args[1];
System.out.println("inputFile 0 "+inputFile);
System.out.println("outputFile 0 "+outputFile);
transformExcelFile(inputFile,outputFile);
//transformExcelFile("E:\\excel\\inputFile.xlsx","E:\\excel\\outputFile.xlsx");
}
}
I use following command to compile and execute my class
E:\excelTest>javac ExcelReadWrite.java
E:\excelTest>java ExcelReadWrite
Error: Could not find or load main class ExcelReadWrite

You have to add the actual Directory to your classpath:
E:\excelTest>java -cp .;%CLASSPATH% ExcelReadWrite

Related

Could not find or load main class while passing class a argument spring batch job

public class demoRunner extends CommandLineJobRunner {
public static void main(String[] args) throws Exception {
// initialize fileappender here
String logFile = args[args.length - 1].split("=")[1];
DemoAppender.initializeAppender(logFile);
CommandLineJobRunner.main(args);
}
}
Run As VM argument : META-INF/spring/student.xml student
student is a job
Location of demoRunner.java: demoBatchJob\src\main\java\com\ncs\sma\runner\demoRunner.java
Location of stduent.xml: demoBatchJob\src\main\resources\META-INF\spring\student.xml
Exception:
Error: Could not find or load main class META-INF.spring.student.xml
Caused by: java.lang.ClassNotFoundException: META-INF.spring.student.xml
Try putting it outside META-INF and add the below VM argument -
spring/student.xml
Run As VM argument : META-INF/spring/student.xml student
You need to select demoRunner as the main class to run and pass the input file and job name META-INF/spring/student.xml student as "Program argument" and not as "VM argument".

Getting error while executing java programs of packages from command line

I'm executing the programs from command line and using packages in them.
my program file names are TestA.java and TestB.java.
I've executed below initially
javac TestA.java
No issues for the above and it generated the class file as well
for the following i'm observing the issue
javac TestB.java
output :
TestB.java:2: error: '.' expected
import TestA;
^
TestB.java:2: error: ';' expected
import TestA;
^
2 errors
and the TestA.java file is
package a.b;
class TestA {
public static void methodPublic(){
methodPrivate();
}
protected static void methodProtected(){
methodPrivate();
}
static void methodDefault(){
methodPrivate();
}
private static void methodPrivate(){}
}
TestB.java content is :
package a.b;
import TestA;
public class TestB {
public static void main(String args[]) {
TestA.methodPublic();
TestA.methodProtected();
TestA.methodDefault();
}
public static void methodPublic() {
}
protected static void methodProtected() {
}
static void methodDefault() {
}
private static void methodPrivate() {
}
}
I'm executing the javac by navigating to b folder where these two files exist.
I'm executing the javac by navigating to b folder where these two files exist.
You don't want to do that; the fully qualified class name of every class includes the package. They form a tree. Much like your filesystem. From the b folder move up two directories (to the folder containing a - e.g. cd ../.. or cd ..\.. on Windows). Then
javac -cp . a/b/TestA.java a/b/TestB.java
Also, you would normally want that to be written to a "binary" output folder. So
javac -cp . -d bin a/b/TestA.java a/b/TestB.java
Finally, you don't need to import TestA because it is in the same package as TestB. But, if you want to you need
import a.b.TestA;

Under one package : compiler fails to read other class

I have three java files in one package : 'Receiver'.
CMReceiverMutant.java
CMReceiverMutantContext.java
TestDriver.java
Here is my TestDriver.java
package Receiver;
public class TestDriver{
public static void main (String[] args){
TestCase1();
// alternateTestCase1();
}
public static void TestCase1(){
CMReceiverMutant obj = new CMReceiverMutant();
obj.INT1SurvFlag();
obj.Exitw0();
System.out.println("Test case 1 reaches state :"+obj._fsm.getState().getName());
if(obj._fsm.getState().getName().equals("CMReceiverMap.Final"))
System.out.println("Test Case 1 passes!");
else
System.out.println("Test Case 1 fails");
}
}
I compiled TestDriver which depends on CMReceiverMutant.java. Eventhough I put them in the same directory. The compiler seems can't read CMReceiverMutant.java and it makes error :
TestDriver.java:11: error: cannot find symbol
CMReceiverMutant obj = new CMReceiverMutant();
^
symbol: class CMReceiverMutant
location: class TestDriver
I use cmd
javac -classpath Receiver\TestDriver.java
and I've tried
javac -classpath Receiver*.java
The errors are the same. Can you tell me whats the problem is?
Thank you
please check "Source" packages in the "Java Build Path" sometimes if the package isn't registered there the compiler fails to load them.
Hopefully it worked for me.

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 solve package does not exist error in java

I am trying to compile my java file name Test.java. Test.java calling a class com.api.APIUser.java which is available in a user.jar file. I have added user.jar in lib folder. But Test.java is unable to pick APIUser.java. While I am compiling Test.java using javac I am getting error
"package com.api does not exist".
Test.java
import com.api.APIUser;
public class Test{
APIUser ap = new APIUser();
ap .login();
public static void main(String[] args){
//to do
}
}
APIUser
package com.api
public class APIUser{
public string login(){
//to do
return string;
}
}
If any one have idea why I am getting this error.please suggest me solution.
Thanks in advance.
put a semicolon after the package com.api like as below
package com.api;
clean and build the project and run if any issue inform
You have multiple issues in your code.
You have no line termination present for the com.api import in the APIUser class;
You have a syntax error in your login method.
Below is the improved code:
import com.api.APIUser;
public class Test {
// APIUser ap = new APIUser(); // This call should be in the method body,
// there is no use to keep it at the class level
// ap.login(); // This call should be in method body
public static void main(String[] args) {
// TO DO
APIUser ap = new APIUser();
ap.login();
}
}
APIUser
package com.api; // added termination here
public class APIUser {
//access specifier should be public
public string login(){
//to do
//return string;//return some value from here, since string is not present this will lead to error
return "string";
}
}
Also be sure that the JAR file is present in the classpath. If you are not using any IDE, you must use the -cp switch along with the JAR file path, so that a class can be loaded from there.
You can use the code below to understand how to compile your class using classpath from command prompt.
javac -cp .;/lib/user.jar; -D com.api.Test.java

Categories