running junit from command line with jar file - java

I have two java file, Point.java and PointTest.java. The work fine in eclipse but I'm trying to compile them through commandline
I downloaded the junit 4.8.2 jar file into the same directory as the .java files
I am currently trying this command to compile them but im getting errors
javac -cp junit-4.8.2.jar:. Point.java PointTest.java
I have tried the command with -cp as well. The first error is
package org.junit does not exist
What am I doing wrong here? If I extract the jar file in the directory, then I am able to compile the java files correctly.
In case it helps, the first few import declarations in the test file are
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;

Could you verify if the name of the junit jar is correct. The default name is junit-4.8.2.jar and not junit.4.8.2.jar

Compile with the -verbose flag so you can see what the compiler is doing. It will print out the classpath.

I suggest looking at the examples in the javac page, particularly Separating Source Files and Class Files. Splitting out source, class files and libraries is common. See, for example, the Maven Standard Directory Layout and Sun Developers Network - Project Conventions. Most IDEs will encourage a similar layout.
Why not do the simple thing and have source files in the same directory as class files? For one thing, if you delete or rename a class, but forget to delete the class file, you can have source code that references the old class. There are a few other gotchas that most of us don't remember because no one puts classes and source files in the same directory.
If you are just experimenting with Java and JUnit I strongly recommend an IDE like Eclipse. For automated builds you might want to consider looking into Ant, Maven or Ivy, but for getting started an IDE has a smaller learning curve with a lot of extra benefits (code completion, debugging, a UI for reviewing JUnit test failures...).

Related

Can we import a class present in /src/main/scala to /src/main/java

I am new to scala guys and I have a scala code in my project, now I wish to add some java parser classes to this proejct and I am trying to keep the Java and scala code separate in my project repository.
I have some parser java classes copied from another repository which I want to keep separate in /src/main/java and the code which is using these parser classes sits in /src/main/scala, so now when I try to import that package which is present under /src/main/java it is not able to locate that class and I am getting compilation error in import. is there a way to import a package present under different java directory into scala class present in scala directory?
Thanks in advance.
You can do this by using the -cp directive with javac so that you can compile from multiple directories.
But the better way to do this would be to compile your scala code separately first and put it in a JAR file. Then you can just include the jar for compilation and runtime
https://www.cs.utexas.edu/~scottm/cs307/handouts/Eclipse%20Help/jarInEclipse.htm

Can a Java class import classes from a third-party project with dependencies managed by Gradle? [duplicate]

I read the Java tutorials on Sun for JAR files, but I still can't find a solution for my problem. I need to use a class from a jar file called jtwitter.jar, I downloaded the file, and tried executing it (I found out yesterday that .jar files can be executed by double clicking on them) and Vista gave me an error saying "Failed to load Main-Class Manifest attribute from [path]/jtwitter.jar".
The guy who coded the .jar file wants me to import it, but where do I store the .jar file to import it in my code? I tried putting both the .jar file and my .java file in the same directory, didn't work.
The file I'm trying to work for is here: http://www.winterwell.com/software/jtwitter.php
I'm using JCreator LE.
Let's say we need to use the class Classname that is contained in the jar file org.example.jar
And your source is in the file mysource.java Like this:
import org.example.Classname;
public class mysource {
public static void main(String[] argv) {
......
}
}
First, as you see, in your code you have to import the classes. To do that you need import org.example.Classname;
Second, when you compile the source, you have to reference the jar file.
Please note the difference in using : and ; while compiling
If you are under a unix like operating system:
javac -cp '.:org.example.jar' mysource.java
If you are under windows:
javac -cp .;org.example.jar mysource.java
After this, you obtain the bytecode file mysource.class
Now you can run this :
If you are under a unix like operating system:
java -cp '.:org.example.jar' mysource
If you are under windows:
java -cp .;org.example.jar mysource
Not every jar file is executable.
Now, you need to import the classes, which are there under the jar, in your java file. For example,
import org.xml.sax.SAXException;
If you are working on an IDE, then you should refer its documentation. Or at least specify which one you are using here in this thread. It would definitely enable us to help you further.
And if you are not using any IDE, then please look at javac -cp option. However, it's much better idea to package your program in a jar file, and include all the required jars within that. Then, in order to execute your jar, like,
java -jar my_program.jar
you should have a META-INF/MANIFEST.MF file in your jar. See here, for how-to.
You need to add the jar file in the classpath. To compile your java class:
javac -cp .;jwitter.jar MyClass.java
To run your code (provided that MyClass contains a main method):
java -cp .;jwitter.jar MyClass
You can have the jar file anywhere. The above work if the jar file is in the same directory as your java file.
You need to put the .jar file into your classpath when compiling/running your code. Then you just use standard imports of the classes in the .jar.
As workmad3 says, you need the jar file to be in your classpath. If you're compiling from the commandline, that will mean using the -classpath flag. (Avoid the CLASSPATH environment variable; it's a pain in the neck IMO.)
If you're using an IDE, please let us know which one and we can help you with the steps specific to that IDE.

Packages not found while compiling java

I'm trying to compile my java file to class.
It is a plugin to a much larger program and it runs fine from eclipse when running from source.
I work in Linux, and in shell I type the following javac MyPlugin.java
This is just some of the output:
MyPlugin.java:11: package javolution.util does not exist
import javolution.util.FastList;
^
MyPlugin.java:12: package javolution.util does not exist
import javolution.util.FastMap;
^
MyPlugin.java:14: package org.apache.log4j does not exist
import org.apache.log4j.Logger;
^
MyPlugin.java:15: package org.jwebsocket.api does not exist
import org.jwebsocket.api.PluginConfiguration;
^
My assumption is that the rest of the errors are caused because the imports cannot be reached.
Do I need to add something to the original command to have these imports included?
You are missing jars from the following projects:
http://javolution.org/
http://logging.apache.org/log4j/1.2/
http://jwebsocket.org/
You can download the missing JARs and include them in your compilation classpath.
Alternatively, you could let maven manage the download and compilation of your application.
Update: Gathering all external jars together in one location
To get all your external libraries in one place you could create a runnable JAR in eclipse using:
Export...->Java->Runnable JAR File
Enter your_temp_jar.jar and click
Copy required libraries into a sub-folder next to the generated JAR
After clicking Finish all the required jars will be in a folder called your_temp_jar_lib.
You should specify the jar files which contains following classes in your classpath.
javolution.util.FastList
javolution.util.FastMap
org.apache.log4j.Logger
org.jwebsocket.api.PluginConfiguration
you can use -cp to specify classpath of jars.
Typing javac at the command line is not a viable or scalable way to build java code. While you could use -cp to javac to add the required dependencies to this compilation, you'd be far better served by learning ant or maven.

compiling multiple java classes in linux

i used netbeans to code the classes and they are all included in a package but when i try to compile the application class in linux it spits out errors on class definitions for the classes i am working with. points at the class names for the objects and says "cannot find symbol" i need help!!!
use javac -sourcepath < your source code path >
Better check -help option as it mostly solve your problems
cd to the directory containing your package then run:
javac -classpath . your_package_name/*
I'm not a Java guru, but I have a small java project that I developed years ago and have recently ported to compile with javac on Linux.
I got this to work in two different ways:
Created a single Java source file that held all of my classes
Put each of my classes in a separate file but all in the same directory
In each case, I can compile and run with the following:
javac *.java && java name_of_main_class
Notice that I did not specify a "-classpath" option when I compiled. I guess this works because I have not used a directory substructure or created a package. If you are dealing with those issues, this page appears to have some examples that may help you: Help with packages in java - import does not work
A key thing to understand about Java packages: They correspond to subdirectories where the classes are defined (or to JAR files which just bundle and compress those subdirectories into a single file). Therefore, anytime you specify the package keyword in your source, you need to make sure that the source files (and the class files) are distributed to subdirectories correspondingly. The -classpath option to javac may provide a workaround when subdirectory structures do not exactly match what is specified by the package keyword.
If you built the project using NetBeans, you can use Ant to build the project on command line. NetBeans generate Ant Build script.
just cd into the directory where the project is located then type 'ant'
it should build the project for you automagically

Using custom .jar's in your java project

I'm importing from two jar's twitter4j-2.0.10.jar and mysql-connector-java-5.1.10-bin.jar, I'm running the code on a server but it's not letting me. I get a..
Streamer.java:9: package twitter4j does not exist
import twitter4j.StatusDeletionNotice;
^
error when compiling. I know this has something to do with getting java to recognise my custom jar's but I don't know how to get java to "see" them.
Any ideas?
You need to set the classpath for Javac.
One possible example (Windows, with your jar files in a folder called "lib"):
javac -classpath lib/twitter4j-2.0.10.jar;lib/mysql-connector-java-5.1.10-bin.jar MyClass.java
That's a very precise example though, your environment could differ considerably.

Categories