0

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.

Azrudi
  • 101
  • 1
  • 7

1 Answers1

0

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'
jingx
  • 3,698
  • 3
  • 24
  • 40
  • `~/test` will also need to be in the classpath. And since the shell will only expand `~` if it’s the first character in an argument, it needs to be first (`-cp ~/test:if97.jar`) or needs to be expanded (`-cp if97.jar:$HOME/test`). – VGR Oct 09 '17 at 19:08
  • @VGR OP's error message suggests that stream.class itself has been found, so I assume OP has somehow copied it to the current directory and doesn't need to have `~/test` on the classpath. – jingx Oct 09 '17 at 19:24
  • Jingx's answer did not work (the error was Could not find or load main class steam) - why? But the comment by VGR worked : 'java -cp ~/test:if97.jar steam', and this also worked : 'java -cp if97.jar:$HOME/test steam'. Thanks for the answer, but I do not understand why VGR's suggestion worked but not jingx's answer. – Azrudi Oct 10 '17 at 14:10