Compiling four java files within one package using javac - 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.

Related

Create .class file from a jar file -java

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

How to compile multiple Java files with Jar dependencies?

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.

Compiling multiple .jar files with .java class

I currently have a java class which I am attempting to compile using Linux terminal along with a folder (/libs) which contains about 9 .jar folders.
I am struggling trying to attempt this.
I am aware of how to compile a single library to a class:
javac -classpath /libs/example.jar MyMainClass.java
just unsure of how this can apply to multiple libraries.
Thanks.
You can either refer to an entire folder containing all the libraries such as javac -cp lib\* MyMainClass.java
Alternatively, you can refer individual libraries as such: javac -cp lib1\Library.jar;lib2\Library.jar MyMainClass.java (separate with semicolon)

how to use JDatePicker jar file into the program

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

compiling a packaged project dependent on a jar

This seems to me to be a trivial question, but I've had a lot of trouble getting an answer.
I've developed a project in eclipse that is dependent on a jar file, which resides in the project's root directory. All my files are in a package "a.b.c" in a src folder. It runs just fine in eclipse. I now want to run this project from the command line. I do this command to compile the project:
javac -classpath dependency.jar -d ./bin/ ./src/a/b/c/*.java
Everything is compiled into class files and put into the bin/a/b/c folder. Then I do these commands to run the project:
cd bin
java -cp ../dependency.jar a.b.c.Main
Now I get "java.lang.NoClassDefFoundError: a/b/c/Main".
So, how do I run a project that is in a package and depends on a jar file?
Just include the current dir in the classpath as well - i.e. java -cp ../dependency.jar:. a.b.c.Main
You also need to specify your compiled files on the classpath, these will contain your a.b.c.Main. On *nix flavor machines the path separator for cp is the colon (:), and on windows it's a semicolon (;), so on *nix, your run command should be (because you're running from the bin directory):
java -cp ../dependency.jar:. a.b.c.Main

Categories

Resources