Shortcut to launch java/scala without specific classpath - 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.

Related

Ensuring Java classes do not import from certain packages

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

Where is the CLASSPATH?

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.

Imports, jars, and heart Ache

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.

How to give the project classpath to a custom Builder in eclipse?

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

Deploying a Java application. How?

I am new to Java (and Eclipse) but I have used .NET (and Visual Studio) a fair amount. I also know about compiling C/C++ code and things like that. I know that at the end I get either an EXE or a nice binary file that can be run from the command line.
I have been making a Java utility that uses some external libraries. I need to compile this into an executable that I can run from the command line on a unix machine, but I cannot find any way to do this.
I can build and run/debug in Eclipse, but that is no use to me as this code will be run on a webserver. I just need all the dependancies compiled in to one file but after hours of searching on Google, the best thing I could find was the Fat-JAR plugin for Eclipse and after using that I just get the following error when I try to run the file:
Exception in thread "main" java.lang.NoClassDefFoundError: Network/jar
This is really confusing me and as it is such an essential thing to be able to do I am sure I must be missing something blindingly obvious, but as I said, after hours of searching I have gotten nowhere.
I really appreciate any help you can give me. Thanks.
If you build your java app using Maven (which can be done with every major IDE), then you can use the maven Shade Plugin to build a shaded jar file; this is a jar file with all of its dependencies included.
A shaded jar can be run from the command line like this:
java -jar myjar.jar command line options
You're doing something standard and you're using eclipse. This means, in your case, Maven is your friend. Download and install the M2Eclipse plug-in. Maven is best at managing dependencies. So, creating a jar with dependencies included will be very, very straight forward. There are thousands of examples on the web and in StackOverflow. If you have problems setting it up, comment on this and I can point you in the right direction.
Sounds like your class path on the server needs to be modified to pick up the jar file containing the Network class. How are you executing your program? What path(s) are you putting in the -cp option?
If you are not sure how to find out the contents inside a jar file, run jar tf , this will list the packaged classes. Validate that one of the jars in your CLASSPATH has that class it says missing.
Give us more details and we can help solve it.
I think I should first explain some basics. A Java class can be run as an application if it has a public static void main(String[] args) method. If it has this method, you can run it from command line as:
java my.package.MyClass <attributes>
Before launching your app, you need to make sure that all required .jar files (and the root of your own class folders, if you did not make a jar from your classes) are in the CLASSPATH environment variable.
These are the absolute basics. When you are building a more complex app, or an app for distribution, you'll probably use maven or ant or some other tool that makes your life easier.

Categories

Resources