I created project in netbeans. In Netbeans it compiles. But i need to know the way of compiling the project in Linux. It simply not just a file. I have folder which contains Build,nbproject,src and etc.
Please tell me the way of compiling a project created in netbeans in linux terminal.
In the most basic case you compile all .java files in the src folder. This generates .class files. You then execute the main class file (which contains a main method). Don't forget to also put all other dependent classes on the classpath.
Compile:
$ cd <yourproject>/src
$ javac $(find . -name "*.java")
Run:
java -cp ./ yourpackagename.YourMainClass
the -cp argument specifies the classpath. All the classes on this path will be considered as dependecies. In this case we put the classpath to ./ which is the current directory (src). That means if MainClass uses other classes in your project, they will be linked (made usable). If you do not specify a classpath your whole application would need to be contained in your MainClass, which is basically never the case.
Another way - much more common - you create a jar archive (basically a zip file) which contains your compiled classes. I'm pretty sure you can generate jar files within NetBeans. That generated jar file can then be executed:
$ java -jar myjarfile.jar
Most people do not directly use the IDE in order to generate the jar but rather use build tools such as Gradle or Maven.
Another option is using the jar command:
https://docs.oracle.com/javase/tutorial/deployment/jar/build.html
Java creating .jar file
Hope this helps.
Related
I am working on a program that automaticly decompiles and compiles java programs. The decompiling works now. But now i am stuck on the compiling of multiple java files in multiple packages. Can anyone help me to find acmd command for compiling a java project?
And if possible make it support external libraries
I hope i did not make that much grammar mistakes English is not my native
language ;-)
Lets say you are at the application root folder trying to compile App.java file from com.example package that uses lib1.jar and lib2.jar libraries from lib folder to a destination bin folder, compilation command should have the following format:
javac -d bin -sourcepath src -cp lib/lib1.jar;lib/lib2.jar src/com/example/App.java
So 'bin/com/example/App.class' file should be created. If App.java uses other classes from the project, they all should be automatically compiled and put into corresponding folders.
I've found an example for running Groovy scripts on systems that do not have Groovy installed using the groovy-all jar file. I attempted the following:
java -cp src:.:lib/* -jar lib/groovy-all-2.0.1.jar src/com/example/MyScript.groovy
The trouble is my script depends on jars in the lib directory plus two other Groovy script files located in src/com/examples. When I run this, it complains about the import statements for all of them. I can run it on a system that has Groovy installed fine by using the following:
CLASSPATH="src:.:lib/*" groovy src/com/example/MyScript.groovy
How do I run Groovy scripts this way, using the groovy-all jar, as well as give it a classpath?
You can't combine both -jar and -cp in a java command, so you need to name the main class explicitly. Looking at the manifest of the groovy-all JAR, the main class name is groovy.ui.GroovyMain, so you need
java -cp 'src:.:lib/*' groovy.ui.GroovyMain src/com/example/MyScript.groovy
(if groovy-all were not already covered by lib/* you would need to add that to the -cp as well).
One way is to compile the Groovy files to "*.class" files. Then include the jar from the $GROOVY_HOME/embeddable directory and put it on the classpath.
Here is a minimalist example (the first line is a simple Unix copy; use whatever works for you):
$ cp /some-dir/groovy-1.8.5/embeddable/* .
$ groovyc Test.groovy
$ java -cp . groovy-all-1.8.5.jar Test
For typical distribution, you would use Ant/Maven/Gradle to build your own jar file with the compiled Groovy (i.e. class files) in it.
I tried to use a sourceforge library in my program.
On my computer I use Eclipse, and I easily add the jar files to my project.
Now, I want to move the code to another computer.
I tried an executable jar file, but the problem is I cannot debug it on the new computer.
So, I decided to move the source code and compile it there.
I tried the following but defeated in all of them: (all in Windows Command prompt)
Copy the jar files in the /lib/ext folder of my jre folder and add this folder to classpath
javac -cp ".\lib\*.jar" src/*.java
javac -cp "./lib/*.jar" src/*.java
In all of them the classes that are defined in the library jar files can not be recognized by java!
Actually the package doesn't find...
Any idea? Any stupid thing that I am doing?
The correct wildcard for matching all jars in a directory is just
-cp "dir/*"
Please see the Understanding class path wildcards section of this page: Setting the classpath
I have a java project in eclipse with different packages. How can I compile the project from command line in unix (bash) ? Using plain javac doesnt seem to work. For eg, in eclipse I have
src
...server
...client
...shared
And the main file that I want to run later is in the server package called server.java
You will have to add all the source paths and dependency .jar files into the compilation call. The easiest way to do this will be to get Eclipse to export an Ant build.xml for the final jar you are trying to compile. This autogenerated ant file is usually pretty messy but gives you an idea of how the project should be built.
javac -sourcepath /path/to/src -d /path/to/classes
When I press "build project" it NetBeans builds, as far as I could find, separate class files. How can I set it to build them into a jar file? Even better it'd be if I can build several applications (all separately runnable from command line) from one project.
I code Scala 2.8.
Using the Run > Clean and Build Main Project command in NetBeans puts your program's JAR in a folder named dist located at the root level of the project; any associated libraries go in dist/lib. As discussed here, a JAR's manifest may have only one Main-Class attribute, but the JAR itself may have an arbitrary number of classes that declare a main() method.
Addendum: Here is a concrete example using H2 Database.
$ java -cp h2.jar org.h2.tools/Console -help
$ java -cp h2.jar org.h2.tools/Server -help
$ java -cp h2.jar org.h2.tools/Shell -help
Addendum: External libraries are often added via the Library Manager.