0

I have my java source files in src/net/... folders and .jar files in lib folder. How to compile and run this files with command line without writing build script ?

3

2 Answers 2

4

Lets say you have your code files in

[someDirectory]
  |
  +-[lib]
  |  |
  |  +-someLib.jar
  |  +-someOtherLib.jar
  |  +-...
  |
  +--[src]
       |
       +-[net]
           |
           +-[name]
                |
                +-[one]
                   |
                   +-[two]
                       |
                       +-[main]
                           |
                           +-Main.java <- code you want to compile
                                          and execute

then if your console is in

someDirectory>

you can compile it with

someDirectory>javac -cp "lib\*" src\net\name\one\two\main\Main.java

but this will produce Main.class file in same directory as Main.java so to execute code from net.name.one.two.main.Main class you would need to include src directory to classPath because this directory contains package that Main class is placed, so you would need to use command

someDirectory>java -cp "src;lib\*" net.name.one.two.main.Main

But it is good practice to separate class files from source files. To do this you can add -d (directory) parameter while compiling pass directory which should have compiled class files. So first create your classes directory at the same level as src directory and execute

someDirectory>javac -d "classes" -cp "lib\*" src\net\name\one\two\main\Main.java

and now to be able to execute your Main class instead creating confusion by src directory to classPath you will have to add classes directory which is more intuitive.

someDirectory>java -cp "classes;lib\*" net.name.one.two.main.Main.java
Sign up to request clarification or add additional context in comments.

8 Comments

Its compile, but how to run them
To run Java app you need to compile class with main method using javac including all required jars in classpath. When you compile it then just use java -cp "lib/*" full.name.of.YourClass from folder that contains your classes structure. Normally it is done by IDE like Eclipse, NetBeans or others.
Before posting that question i tried that but its not working, it says that Error: Could not find or load main class name.one.two.Main but when i remove folder structure its working fine.
Does adding . to classpath with -cp ".;lib/*" help?
To help us help you try including in your question example showing which class you are trying to run, where is it placed, in which directory your console operating (in which directory you are in console, like c:/>, c:/someDirectory>), commands you used till now and exact command that is not working.
|
0

Try this

javac -cp .;lib/lib1.jar;lib/lib2.jar src/net/*.java

lib1 and lib2 are your libraries. It assume your libraries are in lib folder. You may also need to change the destination folder for .class files.

To run the application use

java -cp bin;lib/lib1.jar;lib/lib2.jar net.mypackage.MyMainclass

net...

It assume your .class files are in bin folder.

4 Comments

To Run use java -cp bin;lib/lib1.jar;lib/lib2.jar YOUR_PACKAGE_NAME .It assume your .class files are in bin folder.
But it not working properly program exits by giving several errors
Errors means there are some command line arguments to start the programme. By using your method it does not request them.
Hi MadhuSilva I just tried it and came out that Instead of your package name put the class path like net.mypackage.MyMainClass

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.