how to compile application in java and generate your Bytecodes [closed]

-8

I can not compile my application from the prompt (cmd). Whenever I try, it gives some error. I think I'm doing it wrong because otherwise it would have worked.

    
asked by anonymous 14.12.2014 / 15:28

1 answer

3

Suppose you have your project in a teste folder and your *.java files are in the src folder (with multiple subfolders corresponding to the packages), and you want to put your *.class files in a build folder. Also, there are some JAR s that are libraries that you want to include in the classpath in a lib folder.

That is, let's assume that this is your folder hierarchy is this:

teste
  src
    com
      helloworld
        HelloWorld.java
  build
  lib
    biblioteca1.jar
    biblioteca2.jar

First, go to the root of your project (the teste folder) by navigating using the cd (or chdir ) command.

Then at the prompt enter this:

dir /s /B *.java > sources.txt
javac -cp .\lib\biblioteca1.jar;.\lib\biblioteca2.jar @sources.txt -d .\build

Or, if you are using linux:

find -name "*.java" > sources.txt
javac -cp .\lib\biblioteca1.jar;.\lib\biblioteca2.jar @sources.txt -d .\build

This will compile your project and put everything in the build folder:

teste
  src
    com
      helloworld
        HelloWorld.java
  build
    com
      helloworld
        HelloWorld.class
  lib
    biblioteca1.jar
    biblioteca2.jar

Some comments:

  • If you do not have JAR in your classpath, omit -cp .\lib\biblioteca1.jar;.\lib\biblioteca2.jar .
  • Remember to use a semicolon to separate% s from% s if there is more than one.
  • The JAR file is to ensure that the compiler will grab all the required source files, preventing you from having to type them one by one.

To run your project:

java -cp .\lib\biblioteca1.jar;.\lib\biblioteca2.jar;.\build com.helloworld.HelloWorld

Or if you do not have any file sources.txt as library:

java -cp .\build com.helloworld.HelloWorld

Source: link

    
14.12.2014 / 17:26