I have a bytecode weaver that I want to execute during the eclipse build. To compile/weave classes, it has to have access to the project classpath, including library dependencies and such. But when I go to the project properties->Builders->New->Program dialog, I can't find anywhere to give my weaver the classpath so it can do its job. The variables don't seem to include it.
I would expect at least to be able to configure the equivalent of the Java compiler as a custom builder which would also require all the classpath, so... how do I get it?
Use Ant Builder, not Program builder.
In case of the Ant Builder you can specify a custom classpath.
I found an open eclipse issue that seemed to suggest that what I wanted to do was not implemented. How does one build without the information necessary to build? Oh well.
I found/scripted a workaround:
Use this:
http://java.net/projects/parse-dot-classpath
pass a script the project path so it can find the .classpath file to parse. Pass any variables you might need along with that. Then you can in the script pass that classpath to whatever builder you wanted to use.
Here's an example (not pretty, but it works):
#!/bin/bash
PROJECT_DIR=$3
SCRIPT_DIR=`dirname $0`
CLASSPATH=`cd $SCRIPT_DIR; java $4 $5 $6 ParseDotClasspath $PROJECT_DIR`
java -cp ${SCRIPT_DIR}/classes:${SCRIPT_DIR}/libs/asm-all-2.2.3.jar:$CLASSPATH kilim.tools.Weaver -d $1 $2
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 am new to scala. Now I start to use the testing framework scalatest. But every time I run the test, I need to type scala -cp /path/to/jar/folder/scalatest_2.11-2.2.1.jar my_test.scala to include the scalatest package.
I want to type less. Here is what I think may work.
Put export CLASSPATH=/path/to/jar/folder/scalatest_2.11-2.2.1.jar;$CLASSPATH in .bashrc .
Put alias scalat="scala -cp /path/to/jar/folder/scalatest_2.11-2.2.1.jar" in .bashrc .
But I wanna know what is the common way to let scala/java know package without specific it in CLASSPATH in scala/java community?
The usual way is to use a build system like SBT or Maven (with the scala plugin) to manage your dependencies, classpath and build cycle.
I would like to use the library "Lucene" with java. The instructions to use it tell me I have to put the jar's cointaining the classes inside the CLASSPATH.
The CLASSPATH is the directory containing all the default classes of Java? Or the directory of my specific project? I'm using Eclipse as IDE.
Really confused about that! Thank you.
USEFUL SOLUTION: http://www.avajava.com/tutorials/lessons/how-do-i-use-lucene-to-index-and-search-text-files.html
The Classpath is a collection of directories and JAR files inside which the Java runtime will look for classes.
It can be configured via an environment variable named CLASSPATH, but this usage is not recommended, as it tends to constantly result in problems.
The preferred way to configure the classpath when running a Java program is to pass it via the -cp command line switch when you start the Java interpreter. Usually you write a shell script so you don't have to type it out every time.
If your issue is with using the classes inside the IDE where you write your code, then it depends of course on the IDE. For eclipse, the "Java Build Path" tab of the project properties is where you configure the classpath.
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.
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.