Home > Back-end >  Java code runs through IDE but fails from terminal
Java code runs through IDE but fails from terminal

Time:03-09

I have a Java Code where I am able to run it on Intellij using custom configuration. The configuration has following attributes :-

  • module : java 8 (Oracle OpenJDK 1.8.0_321)
  • classpath : -cp XYZ.main()
  • main : com.ABC.XYZ.ManageTraffic
  • CLI arguements : server XYZ.yml

But when I try to run the jar that was build using gradle from terminal , it gives me Error , could not find or load main class com.ABC.XYZ.ManageTraffic

So far I have tried the following things looking at other solutions at Stackoverflow , still getting the same error

  • java -jar ques.jar
  • java -jar ques.jar com.ABC.XYZ.ManageTraffic
  • java -cp /build/libs/ques.jar com.ABC.XYZ.ManageTraffic

Just to cross check , I unzipped the creataed jar and found that com.ABC.XYZ.ManageTraffic class file is available there , still getting error. What could be the issue?

CodePudding user response:

Run it from the IDE, and while it is running, try to get the command used by the IDE from the process list. not sure what OS you are using but something like this should work on linux/mac:

ps -ef | grep java

After you have the command you can try to understand why its not working for you, or just use that command

CodePudding user response:

Just want to add how I managed to run it. I created a new shaded jar file of the same application. refreshed its dependencies and now it works. I am yet to find out how creating a shaded jar instead of normal jar helped. Right now the only reason I could figure out is there may be version clashes with some dependencies but I wonder how it could throw could not found main class error.

Anyways , then I ran the file with the following command from terminal: java -jar ./build/libs/ques-shaded.jar server XYZ.yml

  • Related