Results 1 to 15 of 15
  1. #1
    Basit56 is offline Member
    Join Date
    Aug 2009
    Posts
    48
    Rep Power
    0

    Default 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.
    Attached Thumbnails Attached Thumbnails How to run program from command line that resides in a packge-command-line-errors.jpg  

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,953
    Rep Power
    19

    Default

    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.

  3. #3
    FON
    FON is offline Senior Member
    Join Date
    Dec 2009
    Location
    Belgrade, Serbia
    Posts
    368
    Rep Power
    5

    Default

    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;
    when you move them do D: disk
    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 :)

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,953
    Rep Power
    19

    Default

    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.

  5. #5
    Basit56 is offline Member
    Join Date
    Aug 2009
    Posts
    48
    Rep Power
    0

    Default

    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;
    As my code used javamail and also Database so i need these two jar files too, so first i add them to my classpath but netbeans was still saying "can not find symbol". So after little bit searching i found that i also add these files to netbeans library so i did it and everything was Ok.

    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.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,953
    Rep Power
    19

    Default

    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.

  7. #7
    FON
    FON is offline Senior Member
    Join Date
    Dec 2009
    Location
    Belgrade, Serbia
    Posts
    368
    Rep Power
    5

    Default

    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

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,953
    Rep Power
    19

    Default

    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.

  9. #9
    Basit56 is offline Member
    Join Date
    Aug 2009
    Posts
    48
    Rep Power
    0

    Default

    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.

  10. #10
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    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.

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,953
    Rep Power
    19

    Default

    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.

  12. #12
    FON
    FON is offline Senior Member
    Join Date
    Dec 2009
    Location
    Belgrade, Serbia
    Posts
    368
    Rep Power
    5

    Default

    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

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,953
    Rep Power
    19

    Default

    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...

  14. #14
    FON
    FON is offline Senior Member
    Join Date
    Dec 2009
    Location
    Belgrade, Serbia
    Posts
    368
    Rep Power
    5

    Default

    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.

  15. #15
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,953
    Rep Power
    19

    Default

    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

  1. Command line argument
    By denisatandi in forum New To Java
    Replies: 8
    Last Post: 10-16-2012, 11:37 PM
  2. Command line arguments help
    By may88 in forum New To Java
    Replies: 8
    Last Post: 12-08-2009, 01:20 PM
  3. Calling a JFrame window from a command line program.
    By new_2_java in forum New To Java
    Replies: 7
    Last Post: 11-09-2008, 03:40 AM
  4. Exporting from the command line
    By o1121 in forum Eclipse
    Replies: 1
    Last Post: 08-09-2007, 07:29 PM
  5. Unable to execute command line command in java
    By LordSM in forum New To Java
    Replies: 1
    Last Post: 08-08-2007, 12:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •