I need to be able to compile and run a Java project from the terminal, and without using something like Maven or Gradle. The project structure looks something like this:
project
src
package1
Main.java
package2
Class1.java
Class2.java
bin
lib
jar1.jar
jar2.jar
Main.java contains the main() method, and it needs Class1 and Class2 in order to run. It also needs the .jar files jar1 and jar2, located in the lib folder. I would like all of the .class files to be in the bin folder.
I've tried my hand at figuring out the terminal command in order to compile and run the program but am a bit stuck. This is the best I've managed to figure out:
cd project
javac -d bin/ -cp lib/jar1.jar:lib/jar2.jar src/package2/*.java src/package1/*.java
This successfully compiles, but then when I try to run Main.java:
java -cp lib/jar1.jar:lib/jar2.jar src/package1/Main
I get the following error:
error: invalid flag: src/package1/Main
Usage: javac <options> <source files>
use --help for a list of possible options
Could someone explain to me what I'm doing wrong?
Related
I am using Java8. I have one .jar file containing .class files.
I downloaded the source code of the same .jar file. And I edited one of the file in it. It is .java file.
Now I want to compile all these .java files in the source code and create a new .jar file with .class files
Any clues?
There are many options
1. if you want to do it from command prompt then you would need to set the classpath and then either create a list of java files with package name and use it for compiling, something like this
# Linux
$ find -name "*.java" > source.txt
$ javac -classpath "${CLASSPATH}" #source.txt
:: Windows
> dir /s /B *.java > source.txt
> javac -classpath %{CLASSPATH}% #source.txt
or use build tool like Ant.
Once you have the .class files, you can use "jar" command to create a jar of the .class files
2. Use IDE - you can use any of these IDEs - eclipe, intellij, netbeans.
You need to setup a project with the java files and compile project and export as jar using class files.
I think it would work out better for you to use an IDE.
1) Compile the classes (javac command)
If you don't have any jar dependencies required at compile time, you should not need to set the classpath.
Otherwise you should specify the cp argument with.
Example (Unix way) :
javac -cp "myLib.jar:myOtherLib.jar" *.java
2) Then create a jar from these compiled class (jar command)
Example :
jar cf myjar myFolderWithCompiledClass
extract your jar to a folder and run this:
javac [folder]/*.java
read that for more info:
https://docs.oracle.com/javase/7/docs/technotes/tools/windows/javac.html
Other similar answers on StackOverflow do not exactly answer the question.
I am running Windows and have two folders: lib and src. lib contains .jar files for dependencies. I attempted to compile via:
javac -cp ".;lib/*" *.java
It worked. Each file has a .class file and the .java file with the main class has an additional Controller$1.class named file.
I attempted to run the program via all of the following:
java -cp ".;lib/*" src/Controller
java -cp ".;lib/*" src/Controller.class
java -cp ".;lib/*" src/Controller$1.class
None of the above methods worked. They all gave me this error:
Error: Could not find or load main class Controller
What is the problem and how do I do this? It works fine in my IDE, but a company I'm interviewing for wants it with the JAR files included and compilable and runnable via command line. I haven't compiled stuff like that in literally ages, so some help will be greatly appreciated.
Additionally, how can I compile and run the things if they have a deeper folder structure. For example if I have the following folders / paths:
lib/
src/
src/core
src/models
src/models/enums
Your project structure seems to be like this:
root:
lib
src: Controller.java and other files without a package
Correct commands:
javac -sourcepath src -d lib src/*.java
java -cp lib Controller
Explanation:
javac -cp ".;lib/*" *.java
java -cp ".;lib/*" src/Controller
You did not copy any class in lib but your "cp" is set to lib. Also the class Controller has no package so:
java -cp "lib" Controller
I believe you want to be using the javac, and not java for those commands you mention, see below:
javac -cp ".;lib/*" src/Controller.java
Also, what are you using to build the project? If you are using Maven, there are some great plugins that will make your life much easier in terms of packaging a JAR with its dependencies.
I have 5 .java files in src folder and many dependent jars in lib folder
While Compiling the .java files with the jars i am facing error as,
src\Grapher.java:12: error: package org.jfree.chart does not exist
import org.jfree.chart.*;
My directory sturcture is,
C:\Grapher\src*.java
C:\Grapher\lib*.jar
I am using the commond javac -cp .;lib/*.jar src/*.java
Make sure you have JFreeChart jar in your lib folder
You have to wrap arguments of -cp to brackets
You didn't provide sourcepath.
Try this:
javac -classpath "lib/*" -sourcepath src src/*.java
Note: this will work only if your java files are located exactly in src folder. In case if you have package declaration in your classes you have to specify package structure.
eg: src/com/company/*.java
I need to use 'JDatePicker' class for a program. I downloaded 'jdatepicker-1.3.2.jar' file. and when i extracted the file using the command "jar xf jdatepicker-1.3.2.jar", I got 3 folders in the directory " META-INF, tutorial, net". But i don't know how to import this to the java program and where to store this jar file and the extracted folders. could any one assist me on the above?
I don't use any IDE. I run the java programs in command prompt
You must have that exact jar file on the class path. When compiling specify jar in the compile
javac -classpath jars\jdatepicker-1.3.2.jar -sourcepath src\project -d classes src\project\MyProg.java
I have four java files in my folder. They are all in the same package. Here's the package declaration
package com.osama.GHide
All of these classes are in the same package. I want to know how can I compile them using javac (i mean i do not know how to compile multiple files that are using each other). And once that is done how do I launch then using java command in the CLI? here are the file names.
EnteringPoint.java
HidingProcess.java
ListFiles.java
From the project's root directory:
javac src/com/osama/GHide/*.java
To run, assuming no other dependencies:
java -cp ./src com.osama.GHide.EnteringPoint
(Assuming EnteringPoint has the normal main function.)
The javac command compiles all the .java files in the package's directory. Since they're all in the same package/directory, this works. It also puts the generated .class files in the same directory, which may or may not be what you want.
To put them in a different directory, use the -d option and supply a path.
javac -d bin src/com/osama/GHide/*.java
Then to run:
java -cp ./bin com.osama.GHide.EnteringPoint
You can run by using command :- javac -cp src/com/**/*.java
I am asuming there is no other dependenices and your root class name would be src only.