Java code runs through IDE but fails from terminal - java

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?

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

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

Java command line using an imported jar

I am sure this is a stupid question and it must have been asked by every java programmer before. But I cannot find a related question at all.
This talks about subdirectories but I don't have any subdirectories as they are all in the same directory as the java file and the directory I executed the command line from Executable jar file error
This solution gives me the same error as I am writing below: Java command line with external .jar
Others (I don't have links to) talk about Eclipse and other IDE but I am not using an IDE, just a Linux terminal.
I am trying to import a public jar file from http://www.hummeling.com/IF97. The downloaded jar file has been renamed to if97.jar.
I have a java file called steam.java with these commands inside the file:
'
import com.hummeling.if97.IF97;
IF97 H2O = new IF97(IF97.UnitSystem.ENGINEERING);
System.out.println("test H2O table PSpecificEnthalpy(1): "+H2O.specificEnthalpyPT(1,300));
System.out.println("test H2O table PSpecificEnthalpy(5): "+H2O.specificEnthalpyPT(5,300));
'
But I do not know how to run this file in the command line.
I successfully compiled by typing:
'javac -cp if97.jar ~/test/steam.java'
Now I have a file called steam.class
But when I execute it with:
'java steam -cp if97.jar'
or
'java steam -jar if97.jar'
I get error:
Exception in thread "main" java.lang.NoClassDefFoundError: com/hummeling/if97/IF97
at steam.start(steam.java:364)
at steam.main(steam.java:341)
Caused by: java.lang.ClassNotFoundException: com.hummeling.if97.IF97
I am trying to execute this in Linux Ubuntu 16.04 using Terminal. Both the files (steam.java and if97.jar) are in the same Home directory where I execute the javac & java command on.
I believe (or I'm mistaken) that the problem is that java isn't able to find the jar file. But I don't know why.
Please advise, thank you in advance.
You need to specify the class name after the JVM options, because whatever coming after the class name are considered arguments for the class, not the JVM.
Try this:
'java -cp if97.jar steam'

Running a java.class file from Linux command line - Oracle JDK8 Installed - openJDK removed

So, I have removed openjdk from my new Ubuntu system and have installed Oracle JDK 8 and Eclipse from their respective websites. I can run a program from Eclipse, however I cannot run it from the command line. I am also not used to using Eclipse (I use NetBeans for my Java class in college.) I noticed that there is no build button in Eclipse. With all of that being said, here is my command line code:
wil#wil-Aspire-E5-521:~/eclipse-workspace/wiltest/src/wiltest$ ls
test.class test.java
wil#wil-Aspire-E5-521:~/eclipse-workspace/wiltest/src/wiltest$ java wiltest.test.java
Error: Could not find or load main class wiltest.test.java
wil#wil-Aspire-E5-521:~/eclipse-workspace/wiltest/src/wiltest$ java wiltest.testError: Could not find or load main class wiltest.test
wil#wil-Aspire-E5-521:~/eclipse-workspace/wiltest/src/wiltest$ java test
Error: Could not find or load main class test
wil#wil-Aspire-E5-521:~/eclipse-workspace/wiltest/src/wiltest$
You need to add the -classpath . command line option.
You can learn more about classpaths here
I believe the command java -classpath . test might work, but it really depends on a number of items that are better explained in the link above.
I actually answered part of my question myself. (For anybody wondering, the classpath is set to be in present working directory by default.) Anyways, I was running java wiltest.test from src and not bin. However, I ran from binary file and it worked. BUT I deleted the .class file thinking that I could change the source file, recompile using javac, and it would create another test.class in the binary folder. It did not. -sigh-

Running jar via command line : classnotfound exception on ONE desktop

I've a java script who's running by several user and working very well.
Today, I asked another user to try the script on his desktop and he's getting a ClassNotFoundException... despite the script is perfectly the same as mine (and jar locations is also the same)
Here's the command tu launch the JAR :
java -cp .;customname.jar;libs/* my.package.MyMainClass
And I also tried to add every jar in the libs folder separately :
java -cp .;customname.jar;libs/lib.jar;libs/lib2.jar;libs/lib3.jar my.package.MyMainClass
And here's the error message the user is getting :
Error: Could not find or load main class ch.vaudoise.hp.services.listener.AutoSysReorder
I checked the JAVA configuration and try to set him the same java version on "Path" environment variable. Same error.
As there's 6 user who can run the script and only one who's getting an error I'm sure it's a configuration issue. But what ? Classpath seems to be OK...
Many thanks for any help..
First things first: You must start by finding where is the conflicting class. If you don't know it, you may find it in two alternative ways:
Programatically: Code this class and execute it with the same classpath (on an environment that does not suffer the problem):
public static void main(String[] args)
{
System.out.println(ch.vaudoise.hp.services.listener.AutoSysReorder.class. getResource("/ch/vaudoise/hp/services/listener/AutoSysReorder.class"));
}
Manually, one by one: Open a command shell and execute:
javap -cp . ch.vaudoise.hp.services.listener.AutoSysReorder
javap -cp customname.jar ch.vaudoise.hp.services.listener.AutoSysReorder
javap -cp libs/lib.jar ch.vaudoise.hp.services.listener.AutoSysReorder
javap -cp libs/lib2.jar ch.vaudoise.hp.services.listener.AutoSysReorder
...
Try one by one every entry in the classpath until the class is found.
Once found the location of the class, open a shell in the conflicting PC and make sure that path is accessible:
dir lib\conflicting-library-or-directory
Also, repeat the javap test:
javap -cp conflicting-library-or-directory ch.vaudoise.hp.services.listener.AutoSysReorder
After this tests, you should have more clues to find the cause of the problem.
Take a look at ClassNotFoundException despite class in the classpath
You are also including meta character (*) in your classpath.
Try without that as suggested in the link.
Also some times copy pasting to command line , may get some characters copied differently.
I cannot add comment as of now : So , editing this answer.
| Java path is not an issue.
Try the following step by step :
- find which jar the class that is being not found is in .
- include only that jar as cp.
- include only that class and try
java -jar that.jar
Also try this once
java -cp "*;"
If you still got issue , probably the jar does not contain the class (You can open jar and check).
And you say script - is this single command which is failing or is it part of script ?. Using java -jar -cp , usually ignores cp.

Having issues installing Hibernate3 on Debian Server

I am attempting to install Hibernate3 onto my linux server but I'm having a hard time figuring out what to do to get it to work. I have tried the following:
copy required jars into a folder and called the folder via "java -classpath etc." and got
java -classpath /home/mcmaster/javalibs/* -jar craftbukkit.jar
Error: Could not find or load main class .home.mcmaster.javalibs.dom4j-1.6.1.jar
load the java command while calling only the core hibernate which resulted in normal class not found error
on a whim installed to system java lib location with no affect
used apt-get to install libhibernate3-java with no change
I have attempted to export $CLASSPATH=~/javalibs which has all the required jars
I'm literally pulling my hair out as an intermediate java user trying to learn some more advanced things(to me).
Here's the error I'm getting when trying to call the Session Class:
Caused by: java.lang.ClassNotFoundException: org.hibernate.Session
Looks like you forgot the quotation marks. Try
java -classpath '.:/home/mcmaster/javalibs/*' -jar craftbukkit.jar (as seen on wikipedia).
EDIT: I am guessing that craftbukkit.jar is executable.

Compiled OK but NoClassDefFoundError when running

I'm trying to send an email using JavaMail API.
I have jdk 1.5 installed in my home directory from self-extracting binary. I'm on Ubintu 9.10
I compile the program using the next command:
~/jdk1.5.0_22/bin/javac -classpath ~/jdk1.5.0_22/jre/lib/javamail-1.4.3/mail.jar:~/jdk1.5.0_22/jre/lib/jaf-1.1.1/activation.jar hw.java
It compiles OK. As you can see I have specified the path to mail.jar and activation.jar
Now I try to run the app using the next command:
~/jdk1.5.0_22/bin/java -classpath ~/jdk1.5.0_22/jre/lib/jaf-1.1.1/activation.jar:~/jdk1.5.0_22/jre/lib/javamail-1.4.3/mail.jar:. HelloWorldApp
I get an exception java.lang.NoClassDefFoundError: javax/mail/Address
Why can it find classes when compiling and cannot do it when running?
How to correctly run my app?
Thanks in advance
That class should come from your mail.jar. I'm not sure your classpath is being parsed properly. The tilde (~) is a shell function and needs expanding before being sent to the Java process. Have you tried removing the ~ and replacing with /home/{whatever} ? I suspect that's the issue.

Categories

Resources