I understand in java that you are forced into a single file per class.
So if I have classes like:
/my_project/main.java
/my_project/classes/user.java
/my_project/classes/other.java
And my main.java references the user and other files, how would I compile this via the command line?
If I was to have external .jar's that I was referencing, and I placed them in a particular folder, how could I also include this in my compiling? (or is there a general place I can put them where they will be picked up automatically like how python does this)
to compile, you will need to specify each source file, from the my_project folder:
javac classes/user.java classes/other.java main.java
You can also specify jar files for your classpath with the -cp option:
javac -cp myjarfile.jar main.java
You may also need to fiddle with the -cp flag to make sure your classes folder is in the classpath.
First of all it's poor style to make Java classes starting with lowercase.
Only public classes need to be in their own file, but you can add as many package-private classes as you like to the same file (although this is seen as poor style).
That said, the easiest way would to compile your code would be:
javac /my_project/main.java /my_project/classes/user.java /my_project/classes/other.java
In any case, proper code layout should be that classes are in a directory structure matching their package.
EDIT: There is a fairly good explanation of conventions here http://www.particle.kth.se/~lindsey/JavaCourse/Book/Part1/Java/Chapter05/packagesImport.html
In addition to the above answer, you can use something like Apache Ant, for easier configuration of your build (if it gets complicated).
Look at the documentation for javac. You can pass multiple source files, or specify the source directory.
Related
I have packages like this:
com.example.pure
com.example.pure.internal
com.example.other
I want to ensure that the classes in the com.example.pure package do not have any dependencies on classes in the com.example.pure.internal or com.example.other packages.
Obviously, I can go through each file manually and eyeball the imports, but I'd like to automate it. I could write some code to do it, but it seems like it might be something that someone else has already solved.
I use Eclipse, so an Eclipse plugin that I could configure to enforce dependency directions would be absolutely ideal, but a command-line utility or Gradle plugin would be great too.
in Apache Lucene they use this handy tool called fobidden-apis to check that certain apis are not used. It is an Ant task so you can call this easily from inside eclipse
you can compile com.example.pure separately alone if it compiles successfully then it's not depends on any other package:
-cp path or -classpath path
or
javac -d bin -cp bin src\...\someclass.java
I have very simple case. In folder D://redbacktree I have two files .java.
I want to compile them into folder D://redbacktree/bin.
What I tried is :
javac -d bin *.java
This created folder redblacktree inside bin folder and put .class files into
D://redbacktree/bin/redblacktree/
But I don't want javac to create redblacktree folder inside bin folder.
How to compile and run this correctly?
I'll create an answer from my comment instead.
So first off; if you have package declarations (which is good practice to organize your code and modules) your files will end up in a folder structure matching your package structure.
I'd suggest reading the man pages for javac and javato see all the flags and options. I'd also suggest using some kind of build tool and follow the java "convention" of how to organize your source and class files. Read more here.
Maven, Gradle or Buildr are three examples of build tools for Java.
Too "fix" your current problem you can run your compiled java classes using:
java -cp bin redblacktree.<name of your class>
i.e you need to use the fully qualified name (package path + class name) when telling java which class to run. Also -cp specifies the (root-)classpath, where your compiled classes can be found.
Maybe your java files have a package declaration?
package redbacktree;
The compiler creates a separate folder for each package. You could try to use the default package (i.e. omit the package declration completely), although it is not a good practice to use the default package.
first off let me start by saying I am completely new to Java, but to give you an idea of how new; I started reading lots of books, examples and so forth and began programming Java using Eclipse about 2 months ago. However, I found a really cool bit of advise about using notepad and the terminal to program instead. Kinda crazy for a newbie to go the hard route, but I love a challenge and I'm serious about learning.
So, In Eclipse I had a really good grasp of how to import, add jars compile etc. When I started using pico and using the terminal (I'm running ubuntu) to compile all went really well, until I wanted to use packages. I've spent two days pulling my hair out because no matter what I do I can't figure it out.
I'm trying to use the acm.jar (which I have many times in Eclipse) however I'm completely lost on how to use it when compiling from the javac in terminal.
So what I'm asking for, is for someone to explain the process getting my jar file to work.
All I'm using to create my java programs is the pico (or notepad) and the javac in the terminal.
To compile and run a java class using external libraries, you have to add that library to the classpath. The classpath is the set of places where the java compiler and the JVM look to find any external libraries/classes that it needs during the process of compiling/executing.
Setting the classpath can be done in 2 ways:
Set an environment variable called CLASSPATH
Set it when your run javac/java
Setting the classpath when running javac/java is done like this:
javac -cp path/to/jar1:path/to/jar2:path/to/jar3:path/to/dirContainingClasses
yourMainClass.java
To run:
java -cp path/to/jar1:path/to/jar2:path/to/jar3:path/to/dirContainingClasses
yourMainClass
: is used as a separator on Linux, for windows use ;
Assuming your source files are in src. Assuming you want your compiled classes to be in classes. Assuming your source files reference classes that are in lib/acm.jar:
javac -cp classes:lib/acm.jar -d classes src/com/foo/bar/MyClass.java
will compile the class com.foo.bar.MyClass and put the generated MyClass.class file in classes/com/foo/bar.
You need the acm.jar file in the classpath. That's what the -cp lib/acm.jar option does. You also need classes in the classpath, because MyClass probably references other classes that you have already compiled and that are in your classes directory.
To run your class, it has to be in the classpath, and acm.jar as well:
java -cp classes:lib/acm.jar com.foo.bar.MyClass
As you see, the classpath contains jar files, and directories containing the folder hierarchy which matches the package hierarchy.
I wouldn't use javac from the command line directly, though. Try using a real build tool, that will build all your classes at once, like Gradle or Ant. Maven is also very popular, but I hate it with passion.
Okay. So here's my question: I am making a data parser in Clojure. One part of my program is that it has to be able to graph the data. I figure, I'll use jFreeChart. However, I have absolutely NO IDEA how to include stuff in JAR files. What I mean is: if I have a app.jar file in my classpath, I don't seem to be able to do:
import app.thing.thing2
without changing the classpath to be inside the jar file.
The idea here is that I don't think I can change my classpath since I need to set it to run Clojure (Or do I?). The global classpath is currently /usr/share/java.
And please don't ask me to use Maven, Ant or any project-building tool unless it is the only way to do this. This is a script for personal use that doesn't need or want a whole lot of overhead.
I wonder if I should just unpack every JAR file, so that I can reference the directory structure? Is this bad?
Let me know if you need any clarifications!
The content of the (Java) CLASSPATH environment variable is available to Clojure so if you add your jar to the global classpath before to run Clojure, you'll "see" it:
export CLASSPATH=/path/to/jfreechart.jar:$CLASSPATH
But, in my opinion, this is not the "clean" way to add a jar to Clojure's classpath (because this makes the library visible to any Java program and may not be desired). Instead, you should use the CLOJURE_EXT environment variable. This is how this variable is documented:
# CLOJURE_EXT The path to a directory containing (either directly or as
# symbolic links) jar files and/or directories whose paths
# should be in Clojure's classpath. The value of the
# CLASSPATH environment variable for Clojure will be a list
# of these paths followed by the previous value of CLASSPATH
# (if any).
On my system, it is defined as below:
export CLOJURE_EXT=~/.clojure
So, to add jfreechart.jar (or any other library) to Clojures's classpath, copy it (or add a symlink pointing to it) in the directory defined in the CLOJURE_EXT variable.
And by the way (I'm sorry but your question is not that clear), if you want to bundle some Java classes into a jar, the command is something like that:
$ jar cf myjarfile *.class
You'll find documentation of jar - the Java Archive Tool - here.
I completely respect your desire not to use a project management tool, though I just spent longer typing this sentence than it takes to set up leiningen. For your one-off script any tool is going to be overkill and Pascal Thivent's answer covers this very well. For people reading this question who perhaps want to produce a jar file, or easily load their Clojure into emacs/slime-swank I cant recommend leiningen too strongly.
If you going to basics you can inline your classpath to include the hardcoded location of your jars, so if you on windows it will look something like
java -cp .;%CLASSPATH%;C:/here/it/is/foo.jar com.foo.MyClass
Not sure how clojure is run, but don't you just add the jar file to the classpath?
i.e.
/usr/share/java:/home/user/myjarfile.jar
I am starting to learn AspectJ. From reading tutorials, I know that I can weave aspects into already compiled class files. However, I just cannot figure out the command that allows me to merge a compiled class file with aspects written on an another file. And one more thing, is it mandatory to have the aspects written in a *.aj file? Thank you
You must add the compiled classes to your inpath. The command line arguments are mostly similar to javac, with some additions. Eg-
ajc -inpath library.jar -sourceroots path/to/sources -classpath $CLASSPATH
The inpath flag can take a jar file, a directory, or a path separated list of either. Also, note that the classes on the inpath are re-woven and new class files are produced in the output directory.
More information here: http://www.eclipse.org/aspectj/doc/released/devguide/ajc-ref.html
As for your second question, yes. Aspects can be in either .aj files or .java files. However, .aj is recommended since these files are recognized by AJDT in the editor. Of course, if you use #Aspect style syntax, you can safely use .java even in eclipse.