I have been away from Java stuff for quite sometime, need some help about basic stuff.
I have the following project structure :
There are no manifest files etc, its just a raw folder structure.
I wanted to know command to compile these java classes from root folder Data Structures - Java and command to execute compiled classes.
I did try
javac -d build com.codesuman.datastructures.Main
This worked first time but failed in next attempt.
Thanks in advance.
I don't see how that could work, even a single time :
javac -d build com.codesuman.datastructures.Main
In javac, the last argument is "source files". It has to specify location of java source to compile in terms of filesystem location : that is file or directory.
But that com.codesuman.datastructures refers to a java package. Something like that is expected : com/codesuman/datastructures/Main.java.
So, to compile that class in the build directory, do that :
javac -d build com/codesuman/datastructures/Main.java
But if Main.java relies on other classes, which looks possible according to your snapshot, you also need to compile these classes.
So a more idiomatic approach in this case is :
javac -d build com/codesuman/datastructures/*.java
But beware the subfolders are not compiled.
Main class seems to be inside linear folder
This should do the job javac -d build com.codesuman.datastructures.linear.Main
Related
this is the folderI am battling to convert classes2-dex2jar.jar.src.java.
So I deleted ONE DRIVE which was recommended. JDK-8u321 and JRE-8u321 are properly installed.
But whenever I say : javac classes2-dex2jar.jar.src.java it's saying javac: not a file. I also
tried javac classes2-dex2jar.jar.src.java\* still it's javac not a file.
this file folder contains many java sourcefiles
this is the command's response
I am unsure about the real situation.
Unzip classes2-dex2jar.jar.
It contains a sub-directory src/java/ with sources typically in a directory tree.
For every package there is a directory: package x.y.z giving x/y/z/.
You would need to call javac inside src/java listing all *.java
with src/java on the class path (-cp .).
(It might be easier to do this in an IDE.)
I have a project with such structure (shortened for simplicity):
and I'm trying to compile it with terminal.
I'm in src directory and firstly doing this:
find * -name "*.java" > sources.txt
After the first command a sources.txt file is generated:
aircraft/Baloon.java
aircraft/Aircraft.java
aircraft/JetPlane.java
aircraft/AircraftFactory.java
aircraft/Coordinates.java
aircraft/Flyable.java
aircraft/Helicopter.java
exception/WrongNumberArgsException.java
simulator/Simulator.java
Further I'm doing:
javac -sourcepath #sources.txt
And it generates all .class files, EXCEPT the first one - Baloon.java is totally ignored by javac.
If I do:
javac -sourcepath #sources.txt src/aircraft/Baloon.java
a Baloon.class file is generated as well with other .class files.
If I manually change first row of sources.txt, for example switch first and second rows, then when I recompile again first .java file in sources.txt is ignored by javac and respective .class file is not created.
Alternatively if I compile with Intellij Idea - everything is fine, no problems occur.
It doesn't matter if I compile in the root directory of project, or in src directory, result is the same - first line of sources.txt is ignored.
So the question is - what I'm doing wrong with above 2 terminal commands? or maybe it's a bug of javac?
javac version - 1.8.0_221
project files themself: https://github.com/Dman-89/42_avaj_launcher
It sounds like your aim is to create a list of source files, and then pass them all to javac to compile. If that is your aim, -sourcepath is not what you want.
You want one of two things:
Do what all real java programmers do
'real' defined as: They do it for money and/or eyeballs, and not as an academic exercise.
Use a build system; gradle or maven are the common choices. They will take care of all this for you far better than a slapdash effort to hack some bash scripts together.
Just pass the java source files
Just remove the -sourcepath part. javac #sources.txt is what you want.
What is going on?
The first 'argument' (first line in your sources.txt file) is the 'value' for the -sourcepath argument, and javac doesn't compile this, because -sourcepath doesn't mean 'compile this stuff' (see later). The rest (lines 2 and further) are just arguments, which is actually what javac will compile.
So what is sourcepath?
To compile source files with javac, you'd write javac foo/A.java bar/B.java. However, what if, say, A.java contains: import lombok.Value;? Javac is now not capable of compiling this file unless javac knows about lombok.Value. Usually such dependencies are already compiled (you have class files in a directory or a jar file), in which case you'd use the -classpath option of javac to tell javac about where to find this stuff.
-sourcepath is similar, except, it's for not-yet-compiled stuff. That means javac will gain awareness of the existence of anything in the sourcepath, in case it comes up that any of that needs to be compiled first, and will only do so if it is needed by any of the actual files you specified for compilation.
Like any -xpath option to javac or java, if you want to specify more than 1 entry, use colons (semicolons on windows) as a separator, not space. Furthermore, the idea is to pass directories and not actual files. And just like -classpath, passing invalid (e.g. non-existing) paths is fine. Then they are just ignored.
Example
javac -sourcepath deps:deps2 src/com/mypkg/Main.java src/com/mypkg/Extra.java
This will tell javac to compile Main and Extra (not deps or deps2 or any files inside). However, if, say, Main.java contains the line: Object o = new bar.baz.Hello();, and the file deps/bar/baz/Hello.java exists, then this command will end up also compiling Hello.java. If deps/bar/baz/Whatever.java also exists, that won't be compiled, unless Whatever is mentioned somewhere in Hello.java, Main.java, or Extra.java. And not in either a comment or just an import statement (import if java-ese for alias, it doesn't actually import anything).
javac will compile the source files specified, e.g. just Baloon.java in your case.
In addition, any classes referenced from those explicitly named source files will also be compiled, if the compiler can find the source files for them. It looks for them on the sourcepath, which defaults to the same as the classpath if not specified.
Assuming Simulator is the class with the main() method (as indicated by the green "play" triangle of the icon), it will directly or indirectly reference all the other source files, so that's the file to specify:
cd src
javac simulator/Simulator.java
Or:
javac -cp src src/simulator/Simulator.java
Trying to compile and run my java program from the commandline that is set up a bit weird. The file structure is as follows:
[ROOT]/
|
|____libs/
| |____myExtraJar.jar
|
|____src/
|____main/
|____com/
|____example/
|____myClass.java
The package is defined at the top of the java file as
package com.example;
I am able to compile the program fine (I think) while in the root folder, using
javac -classpath "/libs/myExtraJar.jar" src/main/com/example/*.java
I don't get any compilation errors (such the ones that occur if I leave off the classpath) and I can see that .class files are being created in the com/example/ folder. However, I can't find any way to run the compiled program. Running
java src/main/com/example/myClass
results in the message
Error: Could not find or load main class src.main.com.example.myClass
Any help would be appreciated.
You need to specify the classpath when you run it, and you also need to use the fully-qualified classname. Like,
java -cp "libs/myExtraJar.jar:src/main" com.example.myClass
Elliot is right. More precisely, you need to add the build directory to your classpath. It is the directory containing your *.class files, and is sometimes named target/.
$ java -cp "target:lib/myExtraJar.jar" com.example.myClass
Moreover, src/main/com/example/myClass should be com.example.myClass, which is the fully-qualified class name. See http://www.manpagez.com/man/1/java/ for details of the java command.
I can't make javac recognize an external .jar file, whose classes I'm trying to extend. I have two files in the same directory: TestConsole.java and acm.jar. I'm compiling from the same directory using the following command:
javac -classpath .:acm.jar TestConsole.java
But it seems like javac is just ignoring acm.jar. It gives me the error:
TestConsole.java:1: package acm does not exist
import acm.program;
^
Of course, acm.program is a package in acm.jar. All of the classes in acm.jar are already compiled; I just want to use them in my classes, not compile them.
What am I doing wrong?
I am running this on a Mac, and the directory structure of acm.jar appears to be valid: It contains an acm/program directory, which has ConsoleProgram.class, the only class that TestConsole extends.
javac -classpath ".:acm.jar" TestConsole.java does not work, either.
javac -cp <jar you want to include>;<jar you want to include> <source.java>
<jar you want to include> if in same directory, just name of jar will do, if not, specify full or relative paths
if more than one jars, separate with ,
replace ; with : on unix
If possible, use some IDE like Eclipse. I used to spend a lot of time on similar things, but in industry, you will hardly ever do it in this fashion.
Are you running these commands on a Windows machine? On Windows, the elements of the classpath are separated by a semicolon, not a colon. So:
javac -classpath .;acm.jar TestConsole.java
Another possibility: the structure of acm.jar is wrong. It's not sufficient that the class files inside were compiled from files that declare package acm.program - the package structure must also be represented as a directory hierarchy, so acm.jar must contain a directory acm, and within that a subdirectory program that contains the actual class files for the classes used in TestConsole.
Check list:
your classes in acm.jar appear as:
acm/program/CLASSX.class
acm/program/CLASSY.class
when decanted with jar tf acm.jar
You're importing them like:
import acm.program.CLASSX ;
or
import acm.program.* ;
Whoever is trying to compile and still having the problem as I struggled for hours, I tried all the answers above and still was not able to run the program due to one minor issue.
The no-brainier issue was the semi colon after every package. I am not sure about Mac or Linux but for Windows Command Prompt this was the case
javac -cp mysql-connector-java-8.0.12.jar; Testing.java
java -cp mysql-connector-java-8.0.12.jar; Testing
You might wanna follow this both cases either in compilation or while running.
Many years behind but i struggled with this syntax, this worked for me to add all jar files plus compile with all classes in the program to the main class
My File Tree:
Store classes .java files
jars .jar files
images .PNG files
command line:
C:\Store>javac -cp "jars/" classes/.java classes/storeMain.java
I'm just adding for folks who are still looking for the answer to the same problem after successful compilation.
While compiling use the command as suggested above by #Michael Borgwardt:
javac -classpath .;acm.jar TestConsole.java
For executing also you need to specify the class path:
java -classpath .;acm.jar TestConsole
So I have to send a java project to someone outside our company who has no experience with java and they need to compile it. Is there an easy way to do it on the windows command line that does not require writing out lists of the files?
Personally I think javac should be smart enough to handle
javac *
when in the folder just under the root of the package hierarchy. Is there anything in this ballpark?
Edit: The source folder structure is complex and the is no single entry class so some of the ideas mentioned so far won't work. Thanks though! Think 9 levels deep with code on many levels.
From the folder that represents the base of your package hierarchy, assuming your entry point class is called Main, in a package called app,
javac -classpath . app/Main.java
should generate the correct class definitions. The compiler will ferret out the dependencies and compile whatever other classes are needed. The class files will appear in the same directory as their source files.
If, as you say, you have 'more than one entry' class, you will have to at least identify all those top level classes from the dependency hierarchy, which can be listed as further params to javac, specifying the packages as they occur. Like so, assuming you also need to start with other.Entry
javac -classpath . app/Main.java other/Entry.java
Note that you will still have to figure out which of your classes are tops of independent dependency hierarchies, whether you are creating an ant script or doing it this way.
javac BaseProgram.java will compile BaseProgram.java from the current directory, and all classes it references that are available in source code, in the same directory tree.
If BaseProgram references Class1 and Class2, and they are available in Class1.java and Class2.java in the same directory, then they too will get compiled. Likewise if they are in a package, and the package directory is available, they will be compiled.
Provide them with an Ant script that does the build with the correct libraries on the classpath, etc. The script can also do other tasks such as building JARs, etc.
This requires that that person downloads and installs Ant, but that is not hard. (And there is nothing to stop you from providing them with an appropriate Ant distro to install. Or even sending them a distro ZIP file that has a copy of Ant "preinstalled" in the tree.)
Providing an Ant script means that you avoid them falling into Java newbie traps such as forgetting to set the classpath, being in the wrong directory, forgetting to recompile dependent files and so on. Plus, it is more "professional".
You can build a file listing all the classes you want to compile (extracted from the javac man page) -
Example - Two Arg Files
You can create two argument files -- one for the javac options and
the other for the source file-
names: (Notice the following lists have no line-continuation
characters.)
Create a file named options containing:
-d classes
-g
-sourcepath /java/pubs/ws/1.3/src/share/classes
Create a file named classes containing:
MyClass1.java
MyClass2.java
MyClass3.java
You would then run javac with:
% javac #options #classes
Or you can use *.java on the command line e.g.
javac greetings/*.java
(Again from man javac)
Or why don't you just compile the source into a jar that your customer can run using their JRE - especially considering they are not Java savvy?
A simple way would be by using:
javac *.java.