Java command line using an imported jar - java

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'

Related

Jar file dont work when i execute my "sh" File in linux consol

When i try to execute to execute this line
bash -x ExecutionAuto.bat
I get an error from my Lib folder saying that one of the jars isnt working properly.
../Demo_Automatisation/lib/SparseBitSet-1.2.jar: line 1: $'PK\003\004': command not found
../Demo_Automatisation/lib/SparseBitSet-1.2.jar: line 2: $'ؔ\220L': command not found
../Demo_Automatisation/lib/SparseBitSet-1.2.jar: line 8: syntax error near unexpected token `)'
��������0 6&»���2<��߽�{�i'���arseBitSet-1.2.jar: line 8: `��6)Ѐ*u)�����RP �rN-p�đ�
�{�eB�M}i��Qa0�{}/�aHU�
'���xQXk)�Ћ.'?l3����A�I�|��0AİV���v�)s ?���5N���V��v��hH�;"���~Gt D��}|ȣ^�`�ܨ"r��d���}7��0t)y���{�W���
+ java org.testng.TestNG ../Demo_Automatisation/testng.xml
Error: Could not find or load main class org.testng.TestNG
Caused by: java.lang.ClassNotFoundException: org.testng.TestNG
I dont know if there is a probleme with the jar files since it works fine in Windows but in linux it doesnt work.
This is my .sh file:
export projectLocation=../Demo_Automatisation
cd $projectLocation
export CLASSPATH=$projectLocation/bin;$projectLocation/lib/*
java org.testng.TestNG $projectLocation/testng.xml
I just coppied what i found on the internet since i had a .bat file at first since i was using Windows does transferring from Windows to linux requieres that i do something to my jar files ?
The issue is that the ; in your classpath definition actually separates two commands so the $projectLocation/lib/* is interpreted as a new command, expands to the name of the jar file and the shell tries to execute the jar file as a shell script (which fails miserably). On Linux the separator for classpaths is : and not ; (almost certainly for exactly this reason).
In other words: instead of
export CLASSPATH=$projectLocation/bin;$projectLocation/lib/*
you need
export CLASSPATH=$projectLocation/bin:$projectLocation/lib/*
As a bonus suggestion: the /* in the CLASSPATH should be interpreted by Java and not the shell, so it's better to actually quote the value:
export CLASSPATH="$projectLocation/bin:$projectLocation/lib/*"

How to include JAR file in a java program that is being executed by command line (Windows)

I know that there are similar posts to this, but I tried all suggested answers there and they didn't work out (maybe I did something wrong). I have a java project with the following folders:
bin
src
in the bin folder, I have few folders with .class files in them (compiled the project with eclipse, now just need to run it with command line). These are the folders inside bin:
client
game
protocol
threadPerClient
tokenizer
I also added the jar file (gson-2.2.1) to the bin folder. My main class is at threadPerClient folder, and this class name is MultipleClientProtocolServer. The main class get an int as a parameter. These are the commands that I tried to run via command line, from bin folder:
java -cp gson-2.2.1.jar threadPerClient.MultipleClientProtocolServer 4000.
And I got for that:
Error: Could not find or load main class threadPerClient.MultipleClientProtocolServer
java -cp threadPerClient/:./ MultipleClientProtocolServer 4000
And I got for that:
Error: Could not find or load main class MultipleClientProtocolServer
java -cp threadPerClient;. com.example.MultipleClientProtocolServer 4000
And I got for that:
Error: Could not find or load main class com.example.MultipleClientProtocolServer
What am I doing wrong?
Make sure you're running command 1. of your question from within the bin folder. Also make sure in -cp you specify the gson jar as well as the current folder:
Unix:
java -cp gson-2.2.1.jar:./ threadPerClient.MultipleClientProtocolServer 4000
Windows should be:
java -cp gson-2.2.1.jar;. threadPerClient.MultipleClientProtocolServer 4000
but I cannot test the command for windows since I don't have a windows machine.

Trying to use Apache Daemon jsvc

I am trying to run a Java program by using jsvc.
I have installed it by
sudo apt-get install jsvc.
To find out a solution, I tried to read the Apache documentation about it (at https://commons.apache.org/proper/commons-daemon/jsvc.html). But this command:
./jsvc -cp commons-daemon.jar:my.jar MyClass
and this other:
./jsvc -cp my.jar MyClass
did not work (of course, I replace the terms by the name of my class etc.).
It gives me the error:
bash: ./jsvc: no such file or directory of this type
So I use jsvc without "./". And I saw here: How to start tomcat with jsvc? that I should use /usr/bin/jsvc
But an other problem is when I use
/usr/bin/jsvc -cp path/to/my/.jar path/to/my/class
nothing happens.
I try the link: How to convert a java program to daemon with jsvc?. But there is something I don’t understand: for the "CLASS =", have I to put a .Main file ? And do I have to put the extension name of the file (for the class and the .jar) ?
I decided to put the .java file which contains my main class (once I putted the .jar, then I didn’t). Then I copied the code, and when I write "esac" and pressed the enter key in the Ubuntu console, the console closed up, and then…nothing.
Has someone already encountered this ?
Are you sure your java installation is in /usr/java?
Beside this, in the second command there's the directory missing. You should do something like that:
export JAVA_HOME=path/to/java/home
./configure
If you don't know where your java installation is located, try this if you are on a mac/*nix, or this if you have windows.

Making a ImageJ .jar from command line

I have modified a ImageJ source code and i have compiled from command line, using the javac command, but Im trying to create a .jar file and I need help.
It is the first time that I'm trying to compile and create .jar from command line. When I created the .jar, I did the next command:
jar cf test.jar ImageJ/ij/*
Directory "ImageJ/ij/" contains the compiled code.
It doesn't fail but when I'm trying to execute the .jar It returns the error "Failed to load Main-Class manifest attribute from test.jar". When I execute a .jar I always do "java -jar file.jar" but, in this case, It doesn't work.
If I execute "java -cp test.jar ij.ImageJ" (ij.ImageJ is the Main Class) It works but I need to execute a macro in Batch mode and... It fails.
java -cp test.jar ij.ImageJ -batch ../MacrosIJ/helloWorld.ijm
Exception in thread "main" java.awt.HeadlessException:
No X11 DISPLAY variable was set, but this program performed an operation which requires it.
...
If I create the .jar in Eclipse It works perfectly, Can someone explain what i am doing wrong?.
If someone can help I will be grateful. Thanks!!
The recommended way to build ImageJ from source is using Maven:
git clone https://github.com/imagej/ImageJA.git
mvn
cd target
java -cp ij-1.50.jar ij.ImageJ

could not find or load main class - Error when run through Command prompt

I know there are similar questions posted before and I went through those but still its not working for me.
I am creating a sample JMS test class (chat application) and uses javaee.jar and javax.jms.jar. I can test it through Eclipse IDE and it works fine. But I am trying to run through command prompt so I can run multiple windows.I managed to compile the Chat.java file and it created the Chat.class. But when i try to run it, I get could not find or load main class. These are the commands I used:
From the src/domain folder:
javac -classpath javaee.jar;javax.jms.jar Chat.java---- this created Chat.Class inside domain folder where domain is the package name
The I ran the following command from src folder
java -classpath javaee.jar;javax.jms.jar domain.Chat ---- this gives me the could not find or load main class domain.Chat error message
But when I run without the -classpath parameter(java domain.Chat), it reads the main() and gives me different error since it cant find the jms jar files.
E:\eclipse\Spring\JMSChat\src>java domain.Chat
Exception in thread "main" java.lang.NoClassDefFoundError: javax/jms/MessageList
ener
So basically it finds the Chat.class file when I don't pass in the classpath parameter and it cannot find the class when I use the classpath to add the jars. I tried running it from within domain folder as well as from src folder, but no luck. Any clue what I am doing wrong?
Thanks in advance.
Try this
java -classpath javaee.jar;javax.jms.jar;. domain.Chat
By default java uses the current directory in the classpath. When you use the -cp flag, it does not so the path to domain.Chat is not found.
1) You can run multiple instances of the app from Eclipse, and you can check their outputs by cycling through their allocated consoles by clicking on the arrow next to the console icon
2) try running from your source folder java -classpath .;javaee.jar;javax.jms.jar domain.Chat "." means current dir

Categories

Resources