2

I've been teaching myself Java, but I have only used Eclipse as a method of compiling and running my project, and would like to see how I would compile and run the following file structure using only the command line (Don't want to simply export a .jar file from eclipse).

The file structure is as follows:

src/
    model/
        file1.java
        file2.java
    app/
        main.java (contains main method)
        file3.java
        images/
            1.gif
            2.gif
    view/
        file4.java
        file5.java

I need the image files compiled in as well, as I used them for a couple of things. I understand the javac command should be used to build the .class files, but I am confused as to who I would compile it (and run) with this file structure (and with the image files). Any help would be appreciated.

Note: I am attempting to compile and run on Ubuntu 12.04 and am strictly trying to use command line.

Tesla
  • 822
  • 2
  • 13
  • 27
  • 2
    use ant or better maven. – user1050755 Mar 14 '13 at 05:46
  • compile the class containing the main method – Shurmajee Mar 14 '13 at 05:48
  • 1
    As `user1050755` says, if you're looking to anymore more than just very trivial toy examples, it would be worth your while to explore `ant` or `maven`. Maven has a steeper learning curl but has enormous benefits if you work on a real project that draws on third-party libraries. If you have very simple requirements, you should still use `ant` over trying to use javac directly. (I'm sure you'd still like an answer to your question, but very shortly it will be time for you to step up to the next level ...) – Greg Kopff Mar 14 '13 at 05:54
  • I will keep that in mind for next time. I'll look into both to see how and where I could apply them. Thanks for the reference. – Tesla Mar 14 '13 at 06:00

3 Answers3

3

Try the following

javac -classpath ./lib/*.jar ./src/*/*.java

Give a proper jar library path, if your java classes use a 3d party libraries. It's better to use a build tool with proper directory structure.

Abimaran Kugathasan
  • 31,165
  • 11
  • 75
  • 105
  • Okay, so the class files would be generated in each respective folder. How would I go about running this program with the class files kind of scattered? – Tesla Mar 14 '13 at 05:57
  • @Tesla If your main class is `com.tesla.SomeClass`, you run java `com.tesla.SomeClass` and java will expect to find it in `com/tesla/SomeClass.class`. The files *should* be scattered in the different folders, since Java uses that to determine their package. – Nathaniel Waisbrot Mar 14 '13 at 06:05
  • That is perfect. I think I was confusing myself a little bit too much. Thanks guys. – Tesla Mar 14 '13 at 06:12
  • `./src/*/*.java ` first * is for all directories right it gave me file not found why please help – shareef Aug 04 '13 at 22:11
  • @Shareef : Please post your directory structure here. – Abimaran Kugathasan Aug 05 '13 at 11:24
  • @KugathasanAbimaran `javac -d bin src\*.java .\src\*\*.java ` or `javac -d bin *.java` my folder structure is `C:\testJavacommand\src` contains simple 2 java classes as main args i mean and folder hello has one simple java too i tried alot of combinations but gives me `C:\testJavacommand\src>javac -d bin *.java javac: directory not found: bin Usage: javac use -help for a list of possible options` thanks alot – shareef Aug 11 '13 at 17:17
0

You could say

javac src/*/*.java

But that will unconditionally recompile, will be annoying with more files, and will break when you have more files than the command line can deal with.

The sensible thing is to use Ant or Maven (Maven is easier/better in most cases). You write a build script (build.xml or pom.xml respectively) and run the builder (ant or mvn).

Nathaniel Waisbrot
  • 23,261
  • 7
  • 71
  • 99
0

Once upon a time, I compiled Java projects with Windows batch files. I remember having to go back and laboriously edit them whenever I added a new package to the project.

With the power of the Linux shell, you can actually make a decent command-line build script without having to do this:

#!/bin/bash

set -e          # quit on error
set -u          # quit if we attempt to use undefined environment variable
set -x          # show commands as they're executed (remove this line if done debugging)

# Run this script from the project root directory

cd src

# Create directory structure
find . -type d | xargs -n 1 -I I --verbose mkdir -p ../dist/class/I

# Use javac to compile files
find . -name "*.java" | javac @/dev/stdin -d ../dist/class

# Copy assets
find . -type f -and -not -name "*.java" | xargs -n 1 -I I --verbose cp I ../dist/class/I

# Jar command
jar cvf ../dist/$(basename $(readlink -f ..)) -C ../dist/class .

You should save this in a file called "build.sh" in your project's root directory. This script is missing a few features. There's lots of verbosity so you can see exactly what's going on. It doesn't handle filenames with spaces or other strange characters very well. If you want a runnable jar file, you have to write a manifest file and include it in the "jar" command. If your project uses any external dependencies outside the Java class libraries, you'll have to add a -cp switch to the javac command line, or export the CLASSPATH environment variables.

The script above will copy the assets into the jar file. This makes a one-file distribution, assuming you extract the assets at runtime. Stackoverflow can tell you more about How to Handle Assets in Single File within a Virtual File System in Java.

As an alternative, you can simply copy the assets from the source directory to the target directory.

As a matter of style, most people keep their assets in a separate directory from source code. E.g. your two .gif files would be under assets/app/images instead of src/app/images. It's not technically necessary to do it this way as long as you can convince your build system to do what you want, but most of the projects I've seen separate them out.

Community
  • 1
  • 1
picomancer
  • 1,786
  • 13
  • 15