2

I'm using Eclipse & Tomcat7 in window7 platform, I have configured project in eclipse also. Usually we run the single java file using CMD. But I want to compile and run the entire java code through command prompt. I've a many structures in single src folder like E:\proj\src\com\mycode.Inside mycode folder there are 7 sub-folder are available & each sub-folder have many .java files & inner-sub-folders. For Example: E:\proj\src\com\mycode\dto\mail.java,E:\proj\src\com\mycode\dto\sms.java E:\proj\src\com\mycode\dto\security\securityFile.java

The above same pattern other folders have java files.so I need to compile & run entire java files including sub-folders & inner-sub-folders USING COMMAND PROMPT.

thanks in advance,

user3114967
  • 639
  • 5
  • 15
  • 38
  • 1
    you can use ant, or simply keep all java files in one directory and use javac -d outdir *.java – BigMike Jan 09 '15 at 14:31

3 Answers3

2

I'll make some (hopefully reasonably safe) assumptions about how your code is structured:

  • you have a main program (I call it com.mycode.dto.Main below),
  • it has compile-time dependencies on the other files (you're not using reflection or whatever),
  • your source files match the package structure (com.foo.Bar is in E:\proj\src\com\foo\Bar.java).

In that case you can do:

javac -d <destination> -sourcepath E:\proj\src E:\proj\src\com\mycode\dto\Main.java

and then the compiler will traverse the file dependencies automatically and output the class files to destination.

David P. Caldwell
  • 3,394
  • 1
  • 19
  • 32
1

If you have sources in multiple hierarchical directory, you may use ant.

create a build.xml file in the root of your project directory.

<project name="MyProject" default="dist" basedir=".">
    <description>
        simple example build file
    </description>
  <!-- set global properties for this build -->
  <property name="src" location="src"/>
  <property name="build" location="build"/>
  <property name="dist"  location="dist"/>

  <target name="init">
    <!-- Create the time stamp -->
    <tstamp/>
    <!-- Create the build directory structure used by compile -->
    <mkdir dir="${build}"/>
  </target>

  <target name="compile" depends="init"
        description="compile the source " >
    <!-- Compile the java code from ${src} into ${build} -->
    <javac srcdir="${src}" destdir="${build}"/>
  </target>

  <target name="dist" depends="compile"
        description="generate the distribution" >
    <!-- Create the distribution directory -->
    <mkdir dir="${dist}/lib"/>

    <!-- Put everything in ${build} into the MyProject-${DSTAMP}.jar file -->
    <jar jarfile="${dist}/lib/MyProject-${DSTAMP}.jar" basedir="${build}"/>
  </target>

  <target name="clean"
        description="clean up" >
    <!-- Delete the ${build} and ${dist} directory trees -->
    <delete dir="${build}"/>
    <delete dir="${dist}"/>
  </target>
</project>

Install ant on your machine, be sure to have its bin directory in your path, then you can just run

ant -f build.xml

Of course this is just a starting point (ant offers several interesting options and let you finetune all the aspects of your build/packaging).

the sample build.xml file was taken from here

BigMike
  • 6,683
  • 1
  • 23
  • 24
  • Already I have ant build.xml and tried with that. But the problem is when I trying to instrument the code using cobertura after compiled with ant build,its showing some error & my application not yet started even through tomcat manager also not start. – user3114967 Jan 10 '15 at 18:25
  • Location: com/cts/itdp/util/licensing/SessionTracker.sessionDestroyed (Ljavax/servlet/http/HttpSessionEvent;) V @70: ifnull Reason:Expected stackmap frame at this location. Expected stackmap frame at this location. Bytecode: 0000000: 033d 11ff ff3e 120d 1100 1eb8 0013 1202 0000010: b800 25bb 0027 59b7 0028 122a b600 2eb8 0000020: 0034 b600 37b6 003b b600 3f12 0d11 0021 0000030: b800 132b b600 4512 47b9 004d 0200 1100 0000040: 213d 1100 003e c600 281c 1100 21a0 0016 – user3114967 Jan 10 '15 at 18:26
  • 1
    that seems unrelated to compilation, probably depends on something missing on your classpath. Try working step by step, first compile, then produce a war, then deploy it and just after you've seen it deploys correctly, use Cobertura. – BigMike Jan 12 '15 at 08:11
0

Try using wildcard:

javac *.java

You could use various parameters as per need basis that comes in handy with javac:

Usage: javac <options> <source files>
where possible options include:
-g                         Generate all debugging info
-g:none                    Generate no debugging info
-g:{lines,vars,source}     Generate only some debugging info
-nowarn                    Generate no warnings
-verbose                   Output messages about what the compiler is doing
-deprecation               Output source locations where deprecated APIs are used
-classpath <path>          Specify where to find user class files and annotation processors
-cp <path>                 Specify where to find user class files and annotation processors
-sourcepath <path>         Specify where to find input source files
-bootclasspath <path>      Override location of bootstrap class files
-extdirs <dirs>            Override location of installed extensions
-endorseddirs <dirs>       Override location of endorsed standards path
-proc:{none,only}          Control whether annotation processing and/or compilation is done.
-processor <class1>[,<class2>,<class3>...]Names of the annotation processors to run; bypasses     default discovery process
-processorpath <path>      Specify where to find annotation processors
-d <directory>             Specify where to place generated class files
-s <directory>             Specify where to place generated source files
-implicit:{none,class}     Specify whether or not to generate class files for implicitly   referenced files
-encoding <encoding>       Specify character encoding used by source files
-source <release>          Provide source compatibility with specified release
-target <release>          Generate class files for specific VM version
-version                   Version information
-help                      Print a synopsis of standard options
-Akey[=value]              Options to pass to annotation processors
-X                         Print a synopsis of nonstandard options
-J<flag>                   Pass <flag> directly to the runtime system
SMA
  • 36,381
  • 8
  • 49
  • 73