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.
Related
I am working on automating Jenkins installations and I'm relying on groovy scripts from $JENKINS_HOME/init.groovy.d .
I want to use my own java library in those scripts. So far I have discovered, that I can do that by putting my jar in $JENKINS_HOME/war/WEB-INF/lib .
The problem is that I want to make the whole process automated and so I am starting with empty $JENKINS_HOME .
java -jar jenkins.war
So if I copy my library before executing the command, then the command deletes it. And copying my jar after executing the command doesn't feel right.
Maybe there's a proper way of doing it? An argument to java or jenkins maybe. I have tried adding my jar to classpath, but it didn't help/
I found 2 ways of using my custom jar in jenkins.
Put the jar inside of jenkins.war
Jenkins will add all jars in jenkins.war/WEB-INF/lib to classpath. So if you want to add contents of custom.jar to classpath you can simply embed it to jar. To do that you should put custom.jar to ./WEB-INF/lib and then execute the following command.
jar --update --file jenkins.war WEB-INF/lib/custom.jar
After that custom.jar will be in you classpath each time you run jenkins.war . jar utility is provided with jdk.
Use --commonLibFolder parameter
jenkins uses embedded jetty to work. By default jetty adds all files from lib directory to classpath. To specify custom directory you can use --commonLibFolder parameter, when jenkins is started. Here is how you can add all jars from libs directory to classpath
java -jar jenkins.war --commonLibFolder=./libs
Pay attention, that using --extraLibFolder will not work, as it adds jars to jetty classloader only.
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.
Can I add a jar file to a Java project I created on my desktop manually without using gradle, Maven or any dependency management tool or any Java IDE? Currently the project is a single folder containing 5 java files and I run it from the terminal. Is it possible to use a jar dependency in this kind of project. If Yes, please show me how.
Look at this answer
Supposing you have in the root directory of your project a Test.jar and a lib directory containing jar files :
Windows
java -cp "Test.jar;lib/*" my.package.MainClass
Unix
java -cp "Test.jar:lib/*" my.package.MainClass
You could use the -cp compiler option.
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 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