I'm in the process of a personal project I'm working on, and I'm trying to import packages I've created in Java.
Say that my directory is C:\Users\B\Desktop\gamingResearch\pokemonGames and in that directory I have the files Battle.java, Move.java, and another folder called pokemon. In my header for Battle.java, I have
import pokemonGames.trainerClass.Trainer;
import pokemonGames.pokemon.*;
And I try to compile the class with javac -sourcepath .:/Desktop/gamingResearch/pokemonGames Battle.java
But I still get an error saying
error: package pokemonGames.pokemon does not exist.
I'm running into the same errors in other classes regarding packages that I've made. Is there something that I'm forgetting to do?
Your sourcepath is wrong.
It should be:
-sourcepath /Desktop/gamingResearch
Hope it helps.
If you program in java, you do need to add libraries (like pokemonGames) to your classpath before compiling your application, like already stated in the comment by cricket_007.
To make things slightly simpler, you could use a build tool like gradle or maven or even just use an IDE (like IntelliJ) to build your application. Please note, that I recommend a build tool as well as using an IDE, so that you are able to build your application on other machines with different IDEs.
Related
I'm very beginner in Java and for that reason I'm having issues to understand how the imports work...
The problem is to import the Ini library to a project in VSC. I've download the jar from ini4j and place it in the folder Referenced Libraries of the project and a .vscode and lib folders were created.
In the class file I try to import it: import org.ini4j.*; and use it.
But when I compile the code it seems like it doesn't find it as you can see here.
Before asking the question I did some research and tried to follow tutorials that said to introduce in .vscode\settings.json this code:
"java.project.referencedLibraries": [
"library/**/*.jar"
"ini4j-0.5.4/ini4j-0.5.4.jar",
"ini4j-0.5.4/ini4j-0.5.4-jdk14.jar"
]
Do you have a solution for my problem please? Is my import poorly done or how I use the library in the call?
It seems like VSC recognises the library but not Windows...
The problem is the way you are compiling the program. If you want to compile it from the terminal and then run it, you need to specify what libraries you are using. I would recommend you to add Java extension to you VS code, but if you insist on compiling it from cmd you need to do:
For Windows machine
javac -cp ".;first.jar;second.jar;third.jar" *.java
And run it as
java -cp ".;first.jar;second.jar;third.jar" Main.java
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
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've always been impressed by the StackOverflow hive mind, and was hoping you could point me in the right direction here.
I've taken some courses in Java programming, and understand how to write a fairly complex Java program. However, I've never learned how to integrate others' software into my own programs.
For a new project, I'd like to integrate a part-pf-speech tagger and chunker into my code, but have no idea how to "load" these programs (if load is the correct term).
I'm certainly not looking for step-by-step instructions, but rather a guide for how to go about this sort of issue. If anyone could get me started in the right direction, I would greatly appreciate it.
Thanks,
Adam
It looks like the externals you want to use are themselves in Java. This means you're in luck - you can use pure java language features to make it work.
There are two things to it:
1) your source files that interact directly with the external libraries have to be imported, or otherwise you'll have to refer to them using the fully qualified classname.
Importing is done with the import statement. These statements should appear right before your class declaration, like so:
import foo.*; //import all classes from the package foo
import foo.bar.Baz; //import only the Baz class from the package foo.bar
public class MyClass {
Baz myBaz = null; //declare a member of type Baz class from package foo.bar
foo.bar.BazBaz myBazBaz = null; //by using a fully qualified classname, I didn't need to write an import statement for foo.bar.BazBaz
}
2) when you compile your sources, the java compiler needs to know where to look for classes you referenced in your source. This is done via the classpath.
The classpath can be a list of just .class files (compiled java classes), but also .jar files (java archives) and .zip files. Typically a project will package all classes it needs in one or more .jar files.
The location of these classes have no bearing on the way you interact with them in java code. It's the compiler's job to read these jars and class files and locate the classes you referred to in your code. If the compiler can't locate the classes you're referring to, you will get a compile time error and you can't compile your program.
You can specify the classpath as an argument to the java compiler command line (http://docs.oracle.com/javase/6/docs/technotes/tools/windows/javac.html#options). However, this becomes unwieldy very rapidly.
Instead, you should use a build tool like ant to do this work for you. The best way to get started is to read this page: http://ant.apache.org/manual/index.html.
From there, go to "Using apache ant" and then to "Writing a simple build file" in its entirety, they explain how to set up the classpath very well there.
You'll need their classes on your classpath when compiling, and again when running your program.
It appears that those projects distribute both src and jars. Grab the jars, and make them available on your classpath. Once your classpath is set up you'll need to import any specific classes/packages that you're using.
See this tutorial on managing the classpath. It covers the basics for command-line compilation/execution; if you're using a particular build system or IDE then the instructions will vary.
Also note the specific instructions at the second link for making data files available. For this they're also using the classpath.
Configure your build path to include the .jar files.
If you're using Eclipse, you would right click on the project file in the Project Explorer, then select Configure Build Path. Finally, add external archive (the ones you've downloaded). Now those functions will be readily available in your program.
Or a more robust way is to create a folder called "lib" in your eclipse project and include all jar files in there. Then from the Configure Build Path window you select those jar files in the lib folder. This makes it easy to share projects with other programmers regardless if they are on Windows or Linux (when adding external jar it saves the absolute path so if something on C:\ will not be found on someone else's PC) Also provides nice integration of dependency libraries on source code managers like GIT, CVS and SVN.
Well typically Java libraries are distributed as a JAR file. Then in your program, you can simply import the new packages and use the provided API.
When you compile and run, you have to make sure that the libraries are included in your class path so they know where to look for the packages.
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.