简体   繁体   中英

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'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM