How to compile and execute this JAVA application in Ubuntu? - java

There is a Hdfs.java inside src/hdfs and there is a utility jar file inside lib.
What are the options I give when I compile the Hdfs.java using the utility jar?
And how do I execute?
I went through the examples here and I'm very confused.

If you do not want to use the compiler inside of NetBeans, then go to your terminal application, set your current directory to src and invoke
javac -cp .:../lib/utility.jar Hdfs.java
To run, you would do this:
java -cp .:../lib/utility.jar Hdfs
If you want to compile and run within NetBeans, see Jigar Joshi's answer.

Looks like you are using net beans, right click on project and do clean and build

Related

How do I compile Java code that uses JAR?

I am trying to compile and run java code that relies on a jar package. I put everything in the same directory and I am still getting this error.
I have no idea why this is. I imported all the jar files to my classpath too.
I guess your code use classes stored in a external jar file.
If you are using command line to compile and execute, you must add the jar file to classpath.
Ex.:
To compile:
javac -classpath C:/folder/MyJarFile.jar MyClass.java
To run:
java -classpath C:/folder/MyJarFile.jar MyClass
Also, make sure you are correctly importing the external classes, as
import thepkg.OtherClass;
I strongly advice to use an IDE, as Eclipse or netbeans do develop anything, even a basic simple example.
Today there are some online IDEs, like Tutorials Point, but I don't know how to include external libs in theses environments.

How to use JFreeChart?

It might be a silly question but I didn't figured it out how to use it.
I have downloaded JFreeChart from
http://sourceforge.net/projects/jfreechart/files/latest/download?source=files
and I don't use Eclipse or Netbeans or Intellij or any other. How can I compile my project within these files on command line ?
Thanks is advance..
Extract zip file you have just downloaded. Copy jars from lib folder to your lib folder and add all the jars to your classpath using -cp switch.
However what are you going to do then? If you do not use IDE you can write code using any editor you want however it is at least 10 times slower than using IDE. Managing dependencies manually and compiling code using command line compiler is possible too but it starts to be extremely complicated and time consuming once you have external dependencies (as in your case).
So, if you want to create something beyond hello world take you time and start working with build tool like maven or gradle and IDE.
Suppose that I have my project structure as following:
hello
src
Hello.java
classes
lib
one.jar
two.jar
In this case I have compile it using command
javac -cp ../lib/one.jar:../lib/two.jar Hello.java
run this command from src folder.
Use ; instead of : if you are on windows.

cannot run java file in command line that created by eclipse

If I create Java file by touch command in command line, edit it using nano, compile it by javac, and then run it, all works well.
But if I use eclipse to build the project, and create file in eclipse. I can compile it using javac, but cannot run it in the command line.
it shows
Error: Could not find or load main class
why this happen? is anything wrong?
sounds like all about right path in which you trying to run your class.
did you use packages? did you run it from bin|target|build dir?
please provide commands and paths from which you trying to execute it

Editing Java files outside of project in Eclipse

I have to write some little programs in Java for school, so I don't want all that stuff that Eclipse generates with a new project. The way I'm doing it now is this:
$ touch myprog.java
open and edit myprog.java in Eclipse
$ javac myprog.java
$ java MyProgClass
The problem is that Eclipse doesn't show warnings and errors while typing the code. It would also be nice if it would let me compile and run the file inside Eclipse (by doing what I do above in the working directory).
Is there any way I can make Eclipse do this?
This is because Eclipse doesn't consider your .java file as something it should compile (and thus generate errors for)
You need to mark the folder containing myprog.java as a source folder. Here's how you do it:
Right-click on the folder and choose "Build Path" -> "Use as source folder".
Well because now it's just a text file for eclipse and it isn't linked to java project.
Create new project and put myprog.java to /src folder in eclipse project.
Then:
$ javac workspace/yourpoject/src/myprog.java
$ java workspace/yourproject/bin/myprog
As a result copy only those 2 files and ignore the rest of project files if you want.
Can't you use an existing project into which to create school classes?
Alternatively the NetBeans IDE is somewhat slimmer. You would use Run File there.
The short answer: no, you can't make eclipse do that.
Eclipse JDT needs to know the classpath to compile (even if that classpath only includes the JRE), builders to tell you if there are errors, and the search engine for standard IDE things like content assist or open declaration.
It's trivial to create one java project, and then use that to create all of your little java programs. They compile correctly, report errors, and are easy to run ... and if you want to run them from the command line as well, there's nothing stopping you.

How to get the complete "java -cp xxxx com.xxx.class" command issued internally by eclipse?

Now my project works inside eclipse, but when I try to run it ,lots of jars are missing.
Alternative: Switch to Navigator view and open the projects file .classpath. All related projects, jars and user libs, that are used by eclipse to assemble the classpath, are listed in this xml file.
Better approach: add the following line to your code and capture the output:
System.out.println(System.getProperty("java.class.path"));
This is the classpath as seen from the applications perspective. You can use the result as a value for the -cp parameter (paths should be absolute).
There are a few option. In the package or project explorer you can right click, select "build path" and then modify the build path. This will open a listing of all jars on your build and execution class path.
Another alternative is to export the project as an executable jar, assuming you are okay with it jarring your program and all of the dependencies into a single jar. I think this is probably the ideal solution for you as it will produce a single file which may be double clicked to launch your application. Or, in a non GUI environment, may be run by executing java -jar MyJar.jar
The "File->Export->Java->Runnable JAR file" allow you to create a runnable jar with all the dependencies present. After invoking this, you will be able to execute your program as
java -jar my.jar
I would strongly suggest - for technical reasons - that you use the "Copy required libraries into a sub-folder next to the generated JAR" option.
You're asking the wrong question here. Eclipse almost certainly doesn't issue a command of that form at all. What you really want to know is what CLASSPATH Eclipse uses when executing your code. That's just a matter of what you have told Eclipse about in the way of library dependencies.
Do this in your code: System.out.println(System.getProperty("java.class.path"));
Oh.. someone beat me to it. Oh well ;)

Categories