how to compile java application and generate your Bytecodes

Asked

Viewed 591 times

-8

I can’t 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.

  • 4

    Buddy, it’s hard to help you if you haven’t told us what your application is, what your mistake is and how you tried to compile it, don’t think?

  • amg just want to know how to compile what I should open the cmd and do what ?

  • 3

    @Marcosmaced just 1) type javac and the name of your source code, 2) enter... But... If it doesn’t work, take a beautiful study in [Ask], in [Tour] and in [Help], and once you understand it properly, come back here and edit the question so we can analyze your problem in a more complete way.

  • 2

    You also need to check that the environment variables are configured correctly.

1 answer

3

Let’s assume you have your project in a folder teste and its archives *.java are in the folder src (with several subfolders corresponding to the packages), and you want to put your files *.class in a briefcase build. Besides, there are some JARs that are libraries that you want to include in the classpath in a folder lib.

That is, let’s assume that this is your hierarchy of folders is this:

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

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

So at the prompt type 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 it all in the folder build:

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

Some remarks:

  • If you don’t have any JAR in your classpath, omit the -cp .\lib\biblioteca1.jar;.\lib\biblioteca2.jar.
  • Remember to use semicolons to separate JARif there is more than one.
  • The archive sources.txt is to ensure that the compiler will pick up all the necessary 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 don’t have any files JAR as a library:

java -cp .\build com.helloworld.HelloWorld

Source:
https://stackoverflow.com/questions/6623161/javac-option-to-compile-recursively

Browser other questions tagged

You are not signed in. Login or sign up in order to post.