Results 1 to 15 of 15
- 01-18-2010, 12:20 PM #1
Member
- Join Date
- Aug 2009
- Posts
- 48
- Rep Power
- 0
How to run program from command line that resides in a packge
Hi,
Hope you all will be fine.I wrote a program that consist of 11 java files and it is in a package named asteriskproject.I have run it on NetBeans IDE and it works fine. I also add two jar in the netbeans library when ran it on NetBeans i.e. mail.jar and mysql-connector.jar. I have also added these two jars in my classpath.
Now i want to run it on Linux. But for experiment first i tried it on windows command prompt. First i copied the folder(asteriskproject) in D:\Check in which my all java files are present. Now the folder become D:\Check\asteriskproject\all .java extension files. Then i opened cmd and go to the D:\asteriskproject and type javac Main.java(main file) but it gives me error. On netbeans it works fine.Certainly i am doing something wrong because it is in a package and also need two jar files.I am attaching an attachment too
Can anyone please guide me how can i run this project using command line because after testing here i am to run it on Linux also.
Thank you.
- 01-18-2010, 03:34 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 26
You run .class files, not .java ones, so the command would be "java Main", not "java Main.java".
Also, netbeans creates a jar file for you (Clean and Build it), which would be easier to distribute, and also sets up (I think) the manifest to say where any dependent jars (eg mail.jar) can be found in relation to your projects jar.
ETA: Yep, under the dist directory created by netbeans will have your jar file and a lib containing all the jars needed to run the app.
- 01-18-2010, 03:59 PM #3
Senior Member
- Join Date
- Dec 2009
- Location
- Belgrade, Serbia
- Posts
- 366
- Rep Power
- 11
TIP:
Pay attention WHERE you copy those source *.java files
and how you run them!
If your src java classes where in some package called :
Java Code:package Check.asteriskproject.all;
create SAME folders as package:
D:\Check\asteriskproject\all
Move to folder 'all' and execute compilation with 'javac':
D:\Check\asteriskproject\all> javac -verbose Main.java
Stay in same folder, and run just created Main.class file,
without extension and using package name:
D:\Check\asteriskproject\all> java Check.asteriskproject.all.Main
If your src classes depend on each other
first you compile them all
--
Using jars is lil more complicated
i used to manually create
META-INF\MANIFEST.MF
and set classpath inside to point to aditions *.jars
Class-Path: . folder3\file3.jar
Also sometimes is useful to copy additional *.jars to ...lib\ext foder like:
'C:\Program Files\Java\jdk1.6.0_16\jre\lib\ext'
so classloader can find them easily
---
hope this will help
regards :)
- 01-18-2010, 05:41 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 26
I wouldn't recommend sticking jar files into the ext folder. That's a crutch, and disguises how you should really be building these things. As I said above, netbeans does all this for you when it jars your project up...may as well use that ability.
- 01-19-2010, 09:44 AM #5
Member
- Join Date
- Aug 2009
- Posts
- 48
- Rep Power
- 0
Hi,
Thanks. As far as i understand Tolls you are saying that use .class files that is made by netbeans. Is it?
FON you described me really well but i tell you how i did all the things. In netbeans i simply made a new Project named FecthingMessages then create a package named asteriskproject and add all java files to this package. My package statement is
Java Code:package asteriskproject;
So you are saying that i made only asteriskproject folder in D(D:\asteriskproject\all java files) not (D:\Check\asteriskproject\all java files) and also some of my java files need jar files although i have added them to classpath but i still need to give some command line parameter for them with my java files while compiling my files.
Can i put my both jar files in the same folder where my java files are present. Will this solve the problem of finding jar files or i still need to give some parameter with my java files.
One question that confuses me as you have seen that my folder contain 11 java files so when i start to compile using javac i need to compile all java files or just compile main method file. Means main is the starting point so compiling main automatically compile all files or i need to compile each file individually.
Thank you.
- 01-19-2010, 12:00 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 26
Netbeans compiles it for you already, unless you've been fiddling with the settings.
The .class files are what the JVM uses, it doesn't understand the .java files, which is why you have to compile the java files.
Finally, do a Clean and Build through netbeans and go to your project directory. There'll be a "dist" folder, which is all you need to distribute your code. It contains the jar files your app needs, and a jar file which *is* your app. "java -jar <jarfile name>" will run it, if you're in that directory.
- 01-19-2010, 04:01 PM #7
Senior Member
- Join Date
- Dec 2009
- Location
- Belgrade, Serbia
- Posts
- 366
- Rep Power
- 11
If you insist on doing it all manually
you have to compile all of them.
I don't see real purpose for that,
as Tolls says Netbeans offers all you need to compile and pack code your code and jars without any trouble,
so just go to that dist folder,
pick your app jar, and run it on different machines and OS's
i don't see why this could work for you
regards
- 01-19-2010, 04:59 PM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 26
Well, not just the jar, but the whole of the dist folder. Need those dependent jars it sticks in the lib directory as well if you're going to distribute it.
- 01-20-2010, 08:00 AM #9
Member
- Join Date
- Aug 2009
- Posts
- 48
- Rep Power
- 0
Hi,
Thanks. Actually what i want to do is to run this code on Linux machine by cron job. Now thanks to you people you give me the solution just pick dist folder and run it. Now i want to ask how I run it on Linux. Suppose i put this dist folder on linux machine in some folder (say /usr/local/src/dist) then by what command i can run it. And one thing more as Tolls said that it also contains the dependent jar in the lib folder and it is, so do i need to add these two jar in my Linux class path or not?.
Thank you.
- 01-20-2010, 08:14 AM #10
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 13
It's just a matter of writing a script that calls
javaw -cp <pathToRequiredJarFile.jarFileName> -jar <jarfileName>
The script syntax depends on your shell scripting language and has nothing to do with Java.
- 01-20-2010, 10:04 AM #11
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 26
Well, it should simply work with:
java -jar <the apps jar file name>
No need for -cp, since Netbeans has already put the relative path into the manifest.
ETA: You should never have to put jar files into your system (or login) classpath. That's not a good way to do things as it will cause trouble should you have more than one app on a machine, possibly requiring different versions of a particular jar.
- 01-20-2010, 03:51 PM #12
Senior Member
- Join Date
- Dec 2009
- Location
- Belgrade, Serbia
- Posts
- 366
- Rep Power
- 11
Once Netbeans creates your app *.jar
forget about dist folder and all other folders in your development environment
Everything you need should be packed for you
and described in MANIFEST file inside *.jar
You just need to run that jar as others told you here,
and AFTER you succeed doing only that,
create you cron script
and put simple line in it which runs your jar
- 01-20-2010, 03:56 PM #13
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 26
Um, no. You still need the structure under the dist folder. Otherwise you'll lose the dependent jars that are held in the lib folder, and referenced in the manifest. In other words, you need the structure under dist...
- 01-20-2010, 06:55 PM #14
Senior Member
- Join Date
- Dec 2009
- Location
- Belgrade, Serbia
- Posts
- 366
- Rep Power
- 11
I'm not interested in how Netbeans works,
but if it does not provide you complete app in jar file in that dist folder,
and you must structure it - you do as Tolls says.
My point was that in the end, when you finish development,
you end up with 1 app jar file that you can run on any OS
just by using simple java -jar myapp.jar command.
- 01-21-2010, 10:10 AM #15
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 26
Except that's not how you deploy jarred apps. You deploy them, generally, as per the dist folder. That is, your app.jar and a lib folder containing your dependencies. Unless you're proposing unzipping the contents of your dependent jars into your app.jar, of course...which strikes me as largely pointless.
Similar Threads
-
Command line argument
By denisatandi in forum New To JavaReplies: 8Last Post: 10-17-2012, 12:37 AM -
Command line arguments help
By may88 in forum New To JavaReplies: 8Last Post: 12-08-2009, 02:20 PM -
Calling a JFrame window from a command line program.
By new_2_java in forum New To JavaReplies: 7Last Post: 11-09-2008, 04:40 AM -
Exporting from the command line
By o1121 in forum EclipseReplies: 1Last Post: 08-09-2007, 08:29 PM -
Unable to execute command line command in java
By LordSM in forum New To JavaReplies: 1Last Post: 08-08-2007, 01:23 AM
Bookmarks