I've used Eclipse for most of my Java programming, but I'm now volunteering at a prison to teach inmates, and they don't have access to any IDE so we have to do everything from the terminal. (I'm also primarily a Math teacher but doing what I can for the CompSci stuff.) As I understand the file structure in Eclipse, when you have a package called, say, fsk, you place it in a hierarchy like
fsk
src
Sort.java
Main.java
bin
Sort.class
Main.class
where the Main class calls on the Sort class, so to compile the source code at the terminal I write
$~/fsk/src: javac Sort.java -d ../bin
The Sort.java file starts with
package fsk;
...
and the Main.java file has the package name and makes a simple call to the Sort class
package fsk;
import java.util.List;
import java.util.LinkedList;
public class Main {
public static void main(String[] args) {
List<Integer> li = new LinkedList<Integer>();
li.add(1);
Sort<Integer> s = new Sort<Integer>();
s.insertionSort(li);
}
}
The Sort.java file seems to compile just fine, but when I try to compile the Main.java file I get the error that it cannot find the symbol "Sort".
If I do all of this without any package declaration, everything works just fine. Besides modeling this after how I understood Eclipse to work I have also been using https://docs.oracle.com/javase/tutorial/java/package/managingfiles.html and thought I was faithfully reproducing the structure and code, but apparently not.
[Edit: Correction, I have been mostly going off of the Eclipse structure as I understand it, I'm now noticing that when the Oracle tutorial structures this it uses a structure like
src
fsk
Sort.java
Main.java
bin
fsk
Sort.class
Main.class
So I suppose my question now is: am I misunderstanding how Eclipse structures the files, and do I need to instead structure them as in the above?]
The structure is fine. You'll want to compile all the Java files with one javac invocation.
javac fsk/*.java -d ../bin
This will get annoying once you start having multiple packages. Long term you should use a build tool such as Maven, Ant, or Make. Compiling files by hand gets mighty tiresome.
Related
First of all thank you for reading my question.
I have a .jar file and .java file which I should compile
They are "fractals.jar" and "FractalsTest.java"
So basically "fractals.jar" contains .class files with packages to run a simple program that displays fractals on a java program, and the directory looks like this.
fractals -- FractalExplorer
| FracalGenerator
| JImageDisplay
+ generator -- Mandelbrot
-- BurningShip
-- Tricorn
So, The top three classes are under directory(folder) and package called "fractals"
the the three fractal patterns under directory(folder_ and package named "fractals.generator"
FractalGenerator utilizes the three fractal pattern classes by importing them, and FractalExplorer class
is the class that contains the main function.
I have succeeded in compiling the six classes and compressed them as "fractals.jar" file with manifest including "Main-Class: fractals.FractalExplorer". Eventually, the "fractals.jar" file runs perfectly fine.
But my goal here is to make another java file that utilizes the "fractals.jar" file like a library to run the same thing!!!
So here is the code of "FractalsTest.java"
import fractals.*;
public class FractalsTest
{
public static void main(String[] args)
{
FractalExplorer fracExp = new FractalExplorer(800);
fracExp.createAndShowGUI();
fracExp.drawFractal();
}
}
I tried compiling it by entering on the cmd like this:
>javac -classpath fractals.jar FractalsTest.java
(assuming they both are on desktop)
It compiles perfectly fine, and "FractalsTest.class" is created.
And now all I need to do is entering
java -classpath fractals.jar FractalsTest
BUT!!!! The console screen shows that
"Could not find or load main class FractalsTest" with "java.lang.ClassNotFoundException: FractalsTest"
Please help!!!What mistakes have I done?????
I guess java -classpath fractals.jar FractalsTest searches for FractalsTest.class inside fractals.jar. Simple java FractalsTest should do the trick.
Edit: This answer states that the current directory must be manually added to the classpath. So add :. at the end of your classpath to include the current directory.
Trying to compile a multi file package. Needing to compile via: javac mainfile.java while also compiling all the other (about 4 other .java files) at the same time through the one file? I have tried using statements like extend and import package.* Any help would be appreciated.
I have used different compiling methods and arguments but trying to do it just by only inserting javac mainfile.java and java mainfile
Summary: Trying to compile multiple java files at once, through java compiling a single file.
My current code for the main file:
package mypackage;
import mypackage.*;
public class mainfile{
public static void main(String[] args) {
Myfile.main(args) //run main from other file
}
}
Edit: Sorry for the lack of information, when compiling, the compiler returns:
MainFile.java:15: error: cannot find symbol
Myfile.main(args);
^
symbol: variable Myfile
location: class mainfile
1 error
javac as a tool does not do what you want. But, that's why other tools exist.
What you're asking for boils down to 'I want a build system'. The vast majority of java projects use maven or gradle.
It would seem like this works fine:
javac -sourcepath src src/mainfile.java
but you'd be deceived. That will merely compile all source files that are directly referenced by the code in mainfile.java, but there are many other ways to refer to code, such as SPI, reflection, XML config files. The vast majority of java projects will end up using some construct that ends up 'breaking' the -sourcepath "trick" sooner rather than later, which is presumably why all java projects use a build system instead of relying on -sourcepath.
Note that all source files act as if they have:
import java.lang.*;
import yourownpackage.*;
at the top, whether you write this out or not. And, import is java-ese for 'alias'. import foo.bar.Baz; means: Whenever 'Baz' appears in this file as a type, assume I meant to write 'foo.bar.Baz', and that is all: import foo.bar.Baz does not run any code that is in the Baz class whatsoever. If you don't use Baz, then the class file produced doesn't mention Baz at all. This will also not cause javac to then compile your entire directory.
I want to be able to use the Apache Commons Math Library in Java but I cannot get it to work correctly and the main site is frustratingly unhelpful (at least for a novice like me) and I haven't been able to find a solution on here yet.
I went to http://commons.apache.org/proper/commons-math/download_math.cgi
downloaded the first option commons-math3-3.6.1-bin.tar.gz
unzipped it and put it into the folder with the java class that I am trying to build.
I then did the command import org.apache.commons.math3;
But I get Error: package org.apache.commons does not exist
Could someone explain (preferably in detail that not even a novice would misunderstand) why this isn't working and what I should do?
Thanks!
First you need to download jar from repository
https://mvnrepository.com/artifact/org.apache.commons/commons-math3/3.6.1
put it to folder test, in same folder create file Test.java with class Test something like this
import org.apache.commons.math3.analysis.function.Abs;
class Test {
public static void main (String ... args) {
Abs abs = new Abs();
System.out.println(abs.value(-10.0d));
}
}
after that compile it with command javac -cp "commons-math3-3.6.1.jar" Test.java
and run it java -cp ".;commons-math3-3.6.1.jar" Test
output will be 10.0
I'm a little lost here. We were given a jar file which contained 3 different .class files in it. I successfully built the path to the jar file, but I don't know how to use them. The class files obviously contain methods that can be used, but I've never worked with a jar file before so I don't know how to use them. I'm not sure if I have to import them somehow, how to instantiate them or anything. I have searched for tutorials to no avail.
All I'm looking for is some guidance so I can get moving on this project. If it is a case where I have to import them somehow how would I do it? For example, I have WikiEdits.class contained in WikiEdits.jar. The name of my main class is P2. Can someone show me a brief example of how this works?
Add the jar to your classpath, if you are using an IDE.
Then, a java class that uses it would look like something like this:
package p2;
import blah.WikiEdits; //references a class in the jar
public final class P2 { //(this is a strange name for a class, by the way)
public static void main(String... args){
//builds a new object of the given class
WikiEdits thing = new WikiEdits();
}
}
If you are using the command line, these examples may help:
http://www.javapractices.com/topic/TopicAction.do?Id=243
you need to add WikiEdits.jat to your path project, then import and instanciate the class.
import WikiEdits
P2 p = new P2();
p.somemethod();
Static class:
WikiEdit.someMethod();
In your java class add the relevant imports from the jar. And then from command line, you can compile and run your class using the classes from jar by definining the right classpath:
Compilation
on windows:
javac -cp .;pathtoyourjar YourClass.java
on linux:
javac -cp .:pathtoyourjar YourClass.java
Execution
on windows:
java -cp .;pathtoyourjar YourClass
on linux:
java -cp .:pathtoyourjar YourClass
If you are using Eclipse then follow this link to know the steps to add jar to your project:
http://www.cs.duke.edu/courses/cps004g/fall05/assign/final/addlibrary.html
I'm about porting a linux tool to windows. The tool works fine on linux system, but now on windows I get this "cannot find symbol" error.
I have this little main class:
package foo;
import foo.bar;
public class Main {
public static void main(String[] args) throws Exception {
bar.getInstance();
}
}
and the error appears now, while doing javac Main.java:
import foo.bar: cannot find symbol
^
symbol: class bar
location: package foo
Main.java and bar.java are in the same directory.
what am I missing?
For one thing, bar should be called Bar to be idiomatic...
Ideally, you should compile from the directory above Main.java, like this:
javac -d out foo/Main.java foo/Bar.java
That will create a directory called "out" containing another directory "foo", which will contain Main.class and Bar.class. So from the parent directory again, you could run:
java -cp out foo.Main
The source locations don't have to match the package structure. You could just call javac from the directory containing Main.java and Bar.java like this:
javac -cp out Main.java Bar.java
(And then run it in the same way as before) However, it's generally a much better idea to structure your source code according to packages.
You may well find it easier to use an IDE (Eclipse or NetBeans, for example) which will handle all the compilation etc for you. If you do want to build a real project from the command line, you should probably look into using a full build system such as Ant or Maven.
(Note that you'd get the same error on Linux as on Windows, if you tried to compile in the same way.)
If any of you have worked on a kotlin project and then working on a complete Java project without the support of kotlin in gradle, and you put a class in kotlin into the Java project and try to import that Kotlin class, you will get this import issue. Not with reference to the exact question, but someone may find it helpful.
Example : trying to import SomeClass.kt in MainActivity.java will throw this error.
I think you need to compile properly the class bar before attempting to compile foo, this is, generate the class file in the proper package structure.