• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to run a Main class with jar library?  RSS feed

 
Leajon First
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have 3 source file below







step1. compile Hello.java and Espanol.java
javac Hello.java Espanol.java

step2. pack Hello.class and Espanol.class into hello.jar which will be used as library
jar cvf hello.jar Hello.class Espanol.class
rm *.class

step3 compile Main.java
javac -classpath hello.jar Main.java

Here is my problem,
step4 run Main.class with my own library hello.jar
java Main(Can not found class Espanol, Hello)
java -classpath hello.jar Main(Can not found Main)

thanks for anyone's any advise.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Leajon First wrote:java -classpath hello.jar Main(Can not found Main)

With that command, you are putting only hello.jar in the classpath. Since class Main is not in the jar, it will not be found. You'll also have to add the directory that contains class Main to the classpath. For example:

java -classpath hello.jar;C:\MyProject\classes Main
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The classpath needs to contain all classes. Something like java -classpath .:hello.jar Main should work. Use a semicolon instead of a colon if you're on Windows.
 
Leajon First
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Solved it, sorry,

 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually you do not require to worry about the classpath, as long as you define the Main-Class attribute while creating the jar.
Then all you need to do is invoke java -jar hello.jar

More details here http://docs.oracle.com/javase/tutorial/deployment/jar/appman.html
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!