This question already has answers here:
javac option to compile all java files under a given directory recursively
(10 answers)
Closed 9 years ago.
I'm trying to compile a Java project from the command line. The project contains class files in different packages. The program compiles and run fine if I specify every java file of every package. Here's my directory structure:
toplevel/
mainFile.java
level1/ (Contains fileA.java)
Now if I do javac -classpath ./toplevel toplevel/mainFile.java toplevel/level1/fileA.java, this compiles fine and I can run it with java toplevel/mainFile.
Now if a create a new folder called 'level2' in level1 and create a class 'B' inside it, the new directory structure becomes:
toplevel/
mainFile.java
level1/ (Contains fileA.java)
level2/ (Contains fileB.java)
To compile this I have to do:
javac -classpath ./toplevel toplevel/mainFile.java toplevel/level1/fileA.java toplevel/level1/level2/fileB.java which is becoming ridiculous. Is there an instruction that recursively compiles each package and the files inside it?
Have you tried -sourcepath flag. From the Oracle documentation,
-sourcepath sourcepath
Specify the source code path to search for class or interface definitions. As with the user class path, source path entries are separated by semicolons (;) and can be directories, JAR archives, or ZIP archives. If packages are used, the local path name within the directory or archive must reflect the package name.
Related
This question already has answers here:
How to compile a java project with a terminal/cmd
(4 answers)
Closed 6 years ago.
This post was edited and submitted for review 1 year ago and failed to reopen the post:
Original close reason(s) were not resolved
My previous question asked how to compile files with the command JAVAC. I still don't know how to set the output files of the compiled source files.
The output of javac is always classfiles, and the name of the file matches the name of the class contained within it. (A source file with multiple classes in will result in multiple output files.)
If you use the -d command line option, javac will also use the package of the class to generate a directory hierarchy, with the -d option specifying the root. Otherwise, each class file will be output in the same directory as the source file from which it was compiled.
From man javac on my system:
-d directory
Sets the destination directory for class files. The destination directory must already exist; javac will not create the destination directory. If a class is part of a package, javac puts the class file in a subdirectory reflecting the package name, creating directories as needed. For example, if you specify -d /home/myclasses and the class is called com.mypackage.MyClass, then the class file is called /home/myclasses/com/mypackage/MyClass.class. If -d is not specified, javac puts the class file in the same directory as the source file. Note: The directory specified by -d is not automatically added to your user class path.
Hope that helps.
I have just shifted back from an IDE to Notepad to write a Java program. The program is using 20 JARs. I compiled successfully. When I decided to run the Java class file using
java -cp ".\\*" MyProgram
it was giving the standard error "Couldn't find or load main class....".
I was confused because when I used to run the java command with all files in an existing folder, it would just get those JARs as the current folder is already in the classpath. As the program is running from the current folder, I tried using -cp "." to include it explicitly in the classpath but that didn't work either.
Finally I was able to run the program with this command:
java -cp ".\\*;." MyProgram.java
I am asking this question to understand the actual logic behind Java's classpath.
Correct me if I am wrong, but I think that the JAR is just a standard archive in which all the packages are encapsulated in respective folders. If all the JARs are in my current folder including my main class file then why can't I run it with:
java -cp "." MyProgram
or simply:
java MyProgram
If the problem is with the multiple JAR files to include and that's why we used ".\\*" to include all the JARs in the classpath, then why do we have to explicitly include the current folder again in the classpath using:
java ".\\*;." MyProgram
To include all jar required to run your program in command prompt use wildcard *:
java -classpath E:\lib\* HelloWorld
You are using "." that defines current directory and "./*" defines all files in current directory.
The class path is a list of jar files and directories containing the classes and resources of your program. Mentioning a jar file adds its contents to the class path.
"./*" will get you only the jar files in the current directory, "." adds the current directory to the class path. This allows to access all classes (and the jar files as raw file resources but not its contents, i.e. the classes contained in them).
If you need both in the class path, you have to specify both.
You've answered your own question, sort of.
. means that it will look for .class files in the current directory.
JARs act just like a directory. So to have the abc.jar "directory" you would specify abc.jar in your classpath.
If you need both the .class files present in the current directory, and the .class files packaged into JARs found in the current directory, you would have the following classpath: -cp ".:*.jar
All the answers here are telling you to use the wildcard without extension (* or ./*) but this is a bad practice, you don't want Java to go look into irrelevant files, so specify the extension: *.jar.
"." means current directory not files in the directory
"./*" means all files in current directory.
So you want to use all jars in current directory so 2nd will work
I have a Java project with 5 packages and 30 classes. I want to test this project on a different computer, but I can't install any sotware on that computer so I can't use things like Maven, Eclipse etc. Is there a way I can execute the program on that computer?
What I tried to do, is to compile the project using Eclipse on my computer, then went to the other computer and tried to execute the project main class via the folder that the main class .class file is at.
I.E., say that the main class name is Hello in package Greetings and Hello.class is at folder named folder. So I opened the command line window at folder and typed the command:
java Greetings.Hello
That didn't work....
Edit: After doing this I got the message: Error: Could not find or load main class Greetings.Hello
If the package name is Greetings and you want to run Hello.class
Hello class must have main method.
Hello.class must in folder name Greetings (package name).
Execute java Greetings.Hello from the one level above of Greetings folder
It seems to me Hello.class is not inside of Greetings folder
If javac is installed on the system you can directly compile on the system. you can compile even large projects including many packages with choosing different options provided by javac.
The javac tool reads class and interface definitions, written in the Java programming language, and compiles them into bytecode class files. It can also process annotations in Java source files and classes.
There are two ways to pass source code file names to javac:
For a small number of source files, simply list the file names on the command line.
For a large number of source files, list the file names in a file, separated by blanks or line breaks. Then use the list file name on the javac command line, preceded by an # character.
Source code file names must have .java suffixes, class file names must have .class suffixes, and both source and class files must have root names that identify the class. For example, a class called MyClass would be written in a source file called MyClass.java and compiled into a bytecode class file called MyClass.class.
Inner class definitions produce additional class files. These class files have names combining the inner and outer class names, such as MyClass$MyInnerClass.class.
You should arrange source files in a directory tree that reflects their package tree. For example, if you keep all your source files in C:\workspace, the source code for com.mysoft.mypack.MyClass should be in C:\workspace\com\mysoft\mypack\MyClass.java.
By default, the compiler puts each class file in the same directory as its source file. You can specify a separate destination directory with -d source
Given that you have Eclipse and you ran the code in Eclipse: the quickest way is to use Eclipse and export it to executable JAR.
If you have Run Configuration (e.g. named Hello) that you use for running the code:
Menu -> Export -> Runnable JAR file
Launch Configuration: Hello
Select export destination: (e.g. C:\tmp\Hello.jar)
Set Extract required libraries into generated JAR
Click finish.
This will create Hello.jar file you can execute typing in:
java -jar Hello.jar
This question already has answers here:
How to run a java class with a jar in the classpath?
(4 answers)
Closed 7 years ago.
I am unable to execute selenium tests(JUNIT) from command line
my project folder path class file
C:\Users\CP042756\workspace\BLR_demo1\bin\com\analytics\logindash
File :LoginTest.class
my project folder path java file
C:\Users\CP042756\workspace\BLR_demo1\src\com\analytics\logindash
File:LoginTest.java
jar file folder: C:\jars\imp\selenium-2.45.0\libs
jar fiLe: junit-dep-4.11.jar
it runs properly in Eclipse
i want to run it in command line
i have tried the following commands from the command line
1)
java -cp C:\jars\imp\selenium-2.45.0\libs\junit-dep-4.11.jar:C:\Users\CP042756\workspace\BLR_demo1\bin\com\analytics\logindash org.junit.runner.JUnitCore LoginTest
Error:Could not find or load main class
2)java -cp C:\jars\imp\selenium-2.45.0\libs\junit-dep-4.11.jar org.junit.runner.JUnitCore LoginTest
Error:Could not find class:Login test
Exception in thread main java.lang.noclassdefounderror
Please help,
You have to use semicolon as path separator in Windows. Then your first example should work.
For class files there are two different rules. check, which one applies to your situation:
For .class files in an unnamed package, the class path ends with the
directory that contains the .class files. For .class files in a named
package, the class path ends with the directory that contains the
"root" package (the first package in the full package name).
So, for the latter try this:
java -cp C:\jars\imp\selenium-2.45.0\libs\junit-dep-4.11.jar;C:\Users\CP042756\workspace\BLR_demo1\bin org.junit.runner.JUnitCore com.analytics.logindash.LoginTest
This question already has answers here:
Java: How can I compile an entire directory structure of code ?
(10 answers)
Closed 7 years ago.
I'm trying to compile multiple .java programs from different directories either on Windows, Mac, or Linux... e.g. cmd or terminal. It doesn't matter.
However, I'm sure that many of you are familiar with how Netbeans stores files in different folders. I have been putting different concepts into different folders, and now I want to run all of them.
For example, a chess program that I have looks sort of like this:
/Chess
/build
/classes
/chess
/Chess.class
/color
/colorHelper.class
/game
/Board.class
/Game.class
/GameManager.class
/Player.class
... etc. (the rest of the directories with .class files)
/build.xml
/gameLog.txt
/manifest.mf
/nbproject
... (some xml and .properties files)
/src
/chess
/Chess.java
/color
/ColorHelper.java
/game
/Board.java
/Game.java
/GameManager.java
/Player.java
... etc. (the rest of the directories with .java files)
So, my question is, how can you use javac *.java (in /src probably) to compile all of the files (because otherwise I get a cannot find symbol error. Since I get a file not found when I run javac *.java in src, I am at a loss.
Thanks in advance,
Dylan
It won't compile in one go.Actually,your src directory doesn't contain any .java file,SO it won't be done in that way!
I am afraid that you'll have to do it by changing your path under src folder to do the same.
You'll have to perform for each chess,color,game,etc. directories to achieve the same.
So,change path at each run or go as advised in the comments to achieve compilation of all the java files.
OR
As proposed by David Ehrmann in your comment,you can do it by compiling in one go using javac $(find . -name '*.java').
It will compile all .java files under your present directory(src(.)).
javac *.java compiles all the Java files within the current directory. In your case, since you have different hierarchy of folders based on the package, you cannot achieve this in one go. At best you can run the command javac *.java on each folder.
You should try using an automated build system like maven or ant. It will allow you to build your project with a single command. It will even allow running unit tests, and packaging your application as a jar (or whatever package you prefer).
If you really don't want to use one of those you can still do it with a single (albeit lengthy) command. The *.java is just a wildcard in a file path. Its looking for any files in the current directory ending with .java. On linux you can try something like this find -name "*.java" | xargs javac. The first part is a find command that searches recursively for all files ending with .java. It this pipe's that list of files to xargs which adds them all as individual arguments to the java command.
That really isn't the best way. Again, try maven. Its pretty simple to set up for a basic project, your IDE usually has a feature to create a maven project for you as well.
to compile more than one file having different package name in same folder use
javac -d . *.java