• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to deploy a core java application with dependencies on a linux server?  RSS feed

 
Ranch Hand
Posts: 922
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know the way to deploy a web application is to put its war in the deploy folder and thats it. But how to deploy a core java application. My application has is a core java applicaiton such that it has the below:

1) It a class with Main method that is the entry point of the application.

2) The above class(Class with Main method) does all the functionality but depends on some external jars.

3) This class also uses other classes which are in this package.


Now I want to run this class from a scheduler which runs every 1 hour.

The program is working fine but I cannot take entire code on the production server. So should I create 1 jar and deploy it by making it executable jar Or any other strategy? Please advice.

thanks
 
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why can't you put the code on the server? Using a cron job to run background jobs is a fairly normal thing to do.
 
Monica Shiralkar
Ranch Hand
Posts: 922
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.Do you mean to say that I should place all the .class files on the server? So I do not have to make a jar of my application and transfer to the production server?

So if my code has say 100 java classes and some libraries(jars), I need to transfer 100 .class files to the server and the library jars.?
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you should definitely put all the loose class files into a jar. And you need to transfer all the dependent jars as well, yes. Neither part should present a problem.
 
Monica Shiralkar
Ranch Hand
Posts: 922
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.

So you mean I should transfer only jars to the server and no .class file. Is that correct?

There are around 100 classes out of which one has the entry point and main in a folder and these have dependent jars.If I make a jar from all classes in this folder will it understand the dependencies that the main class calls other classes and that it uses other jars. How to make this jar understand these dependencies? So does that mean finally there will be 1 executable jar which I will run and it will do everything?
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whether all your classes are in loose class files or in a jar file makes no difference at all. You need to set up the classpath anyway, and this would be an extremely minor point.

I don't recommend to put all the classes in all the other jar files into the same jar file as your classes. There is no good reason to do so in this case.
 
Monica Shiralkar
Ranch Hand
Posts: 922
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to set up the classpath anyway, and this would be an extremely minor point.


Right now I have a unix script at the top of which I have set JAVA_HOME, set CLASSPATH(to include dependencies).
Then I am calling the java . -Classpath com.MyClass


(MyClass is the entry point with main method which does everything)

So now this MyClass should be in a jar which has other class jars? Is it the case? Will it be an executable jar?How has the classpath to be set in that case. ?
 
Marshal
Posts: 56608
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java Tutorials section.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't use the CLASSPATH, use the "-cp" switch of the "java" command. Then put all the jar files into one directory, and include the directory in the classpath - done!
 
Sheriff
Posts: 22846
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I'm not mistaken, you still can't just put the directory into the classpath and have all jars in it be part of the classpath. But as of Java 6 you can provide a wildcard to do that, like this:



which would mean to include all jars in the "/application/jars" directory as part of the classpath.
 
Monica Shiralkar
Ranch Hand
Posts: 922
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks.

So if I do jar -cvf test.jar com/*.class. Will that understand the dependencies too? That is my concern.
(com is the folder with all classes interdependent on each other and some external libraries)


Another thing I tried is a small example to check this:


To test this I created Temp.java compiled it to get Temp.class

Ran it using java Temp command: it ran fine.

Then I created jar of the classpath using jar cvf command.Then I removed the .class file and ran the java command. The response was:

D:\temp>java com.mkyong.common.action.Temp
Exception in thread "main" java.lang.NoClassDefFoundError: com/mkyong/common/act
ion/Temp
Caused by: java.lang.ClassNotFoundException: com.mkyong.common.action.Temp
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: com.mkyong.common.action.Temp. Program will exit
.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Will that understand the dependencies too?

No, those will be separate. That's why I said to set up the classpath so that it points to a directory - in which ALL jar files go, your own one, and all the dependencies.

To create and use jar files, the commands would be something like "jar -cvf test.jar com" and "java -cp test.jar com.mkyong.common.action.Temp". http://docs.oracle.com/javase/tutorial/deployment/jar/basicsindex.html has all the details.
 
Monica Shiralkar
Ranch Hand
Posts: 922
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks. I will try doing that.


One doubt which I have is I created a jar using class file of a simple hello world program. When class file was present I could run it. When I create jar and deleted class file it gave error as below:

Steps I did:

So if I do jar -cvf test.jar com/*.class. Will that understand the dependencies too? That is my concern.
(com is the folder with all classes interdependent on each other and some external libraries)


Another thing I tried is a small example to check this:


To test this I created Temp.java( a java hello world) compiled it to get Temp.class

Ran it using java Temp command: it ran fine.

Then I created jar of the classpath using jar cvf command.Then I removed the .class file and ran the java command. The response was:

D:\temp>java com.mkyong.common.action.Temp
Exception in thread "main" java.lang.NoClassDefFoundError: com/mkyong/common/act
ion/Temp
Caused by: java.lang.ClassNotFoundException: com.mkyong.common.action.Temp
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: com.mkyong.common.action.Temp. Program will exit
.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Both the "jar" command you used as well as the "java" command" were incorrect - that's why I posted the correct forms.

After reading through the pages I pointed you to, you should learn to use "jar -tf ..." to look into jar files in order to learn what's in them, and to check whether the ones you created were created correctly.
 
Monica Shiralkar
Ranch Hand
Posts: 922
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually I followed below tutorial where they gave command syntax as :jar cvf TicTacToe.jar TicTacToe.class. I tried the same way.

http://docs.oracle.com/javase/tutorial/deployment/jar/build.html
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you used "jar -tf" on the jar file you created, did it contain what you thought it would contain? I'm fairly certain it didn't, which is why I posted what you should do to create it.
 
Monica Shiralkar
Ranch Hand
Posts: 922
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did jar -tf Temp.jar


D:\temp\Temp>jar -tf Temp.jar
META-INF/MANIFEST.MF
.project
.classpath
com/Temp.class

D:\temp\Temp>java com.Temp
Exception in thread "main" java.lang.NoClassDefFoundError: com/Temp
Caused by: java.lang.ClassNotFoundException: com.Temp
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: com.Temp. Program will exit.


Despite this I got the error.

If I copy the .class file here and run java com.Temp it runs fine.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, so you renamed the package from "com.mkyong.common.action" to be just "com". That's not necessary to make jar files work, but doesn't really make a difference.

But you're still not using the command I posted - it's missing the "-cp" switch that includes the jar file in the classpath.
 
Monica Shiralkar
Ranch Hand
Posts: 922
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks

using java -cp Temp.jar com.Temp worked for the example.

Now I need to convert the application into jar and run this way.

I am calling the application from a shell script as below:

run.sh



And it works. The problem is I do not want to keep the class file for MYMainClass and other classes in the production server. I want to keep jar
How can I do it.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now that you know how to create a jar file, what specific difficulty are you encountering in creating one, transferring it to the server, and adding it to the classpath in your script?
 
Monica Shiralkar
Ranch Hand
Posts: 922
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I went to the folder where all classes including the MainClass is. After this I ran the command java cf Temp.jar /*.class

After this I saw the contents of jar using java tf command. i could see MainClass.class too.

Now I ran the command java -cp Temp.jar com.MainClass

It gave a message Error: Could not find or load main class com.MainClass
 
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Monica. Shiralkar wrote:I went to the folder where all classes including the MainClass is. After this I ran the command java cf Temp.jar /*.class

After this I saw the contents of jar using java tf command. i could see MainClass.class too.

Now I ran the command java -cp Temp.jar com.MainClass

It gave a message Error: Could not find or load main class com.MainClass


Question: When you saw the "MainClass.class" file, was it in the "com" subdirectory? You can't just jar class files without putting them into there correct package directories.

Henry
 
Monica Shiralkar
Ranch Hand
Posts: 922
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of giving jar name in command I gave it in Classpath variable above and it changed the error message:

/MyApp.jar: /MyApp.jar: cannot execute [Exec format error]
Error: Could not find or load main class com.MyMainClass


shell script is as:



I know I should give java -cp MyApp.jar com.MyMainClassName

but I also have to include other things too so tried this way.

 
Monica Shiralkar
Ranch Hand
Posts: 922
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes it is in com directory.I tried the java -cp JarFileName command again but got errror


I Tried the script



Error message:


MyApp.jar: cannot execute [Exec format error]
Error: Could not find or load main class com.MyMainClass


 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Monica. Shiralkar wrote:Yes it is in com directory.


I asked because you said this...

Monica. Shiralkar wrote:I went to the folder where all classes including the MainClass is. After this I ran the command java cf Temp.jar /*.class


And that jar command won't create a "com" directory in the jar file.

Henry
 
Monica Shiralkar
Ranch Hand
Posts: 922
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually that was first to run a simple program with the help of Ulf Dittmer to see if it works and then trying it to the real program which I am doing now.

My latest error message received is

./run.sh
Error: Could not find or load main class com.MyMainClass



I am running it from the same folder where MyApp.jar is present which has com.MYMainClassName
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Monica. Shiralkar wrote:
./run.sh
Error: Could not find or load main class com.MyMainClass

I am running it from the same folder where MyApp.jar is present which has com.MYMainClassName


Please list the contents of the jar file, and post it to this topic.

Henry
 
Monica Shiralkar
Ranch Hand
Posts: 922
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jar -tf MyApp.jar
META-INF/
META-INF/MANIFEST.MF
MyClassName.class

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Monica. Shiralkar wrote:jar -tf MyApp.jar
META-INF/
META-INF/MANIFEST.MF
MyClassName.class


As previously mentioned, it needs to be in the com directory. Your listing needs to look like this...

META-INF/
META-INF/MANIFEST.MF
com/
com/MyClassName.class


And also, is your class MYMainClass or MyMainClassName? Your commands and errors messages don't seem consistent.

Henry
 
Monica Shiralkar
Ranch Hand
Posts: 922
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
those were in com directory but then after making jar I moved it to the directory from where I am running the shell script.


I just copied the jar once again and created a new folder this time and placed it. I created a new script and pasted the old contents

and it WORKED.

I do not know that just by copying it again to another folder how it worked but it worked.

Thanks a ton Ulf Dittmer and Henry Wong
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!