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
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 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
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
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.