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
Related
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.
I have written a Java program with intellij and it's working, but now I want to run this file with a command, but when I try to compile it with javac I get errors because it doesn't recognise my imports, I'm new to programming so how can I run my program knowing that it has dependencies in maven?
If you have a choice, you should use maven command (mvn) rather than the low-level compiler javac, it makes compiling and packaging much easier.
But if you want to use javac, you must pass the path to the jar your code depends on, as explained in : How do I compile a java file that has jar dependencies?
If you have pulled your dependencies using maven and you are using centos, the jars should be stored in directory ~/.m2
But once again you should consider installing and using maven, if you want to manually compile your code in a similar way Intellij does
Maven was created to make it easier to handle builds with dependencies (jar files) which can be automatically downloaded from the internet. This includes invoking javac with all the dependencies on the build classpath using "mvn compile" - you do not have to invoke javac yourself! Look in target/classes for your byte code files compiled by Maven.
You can also ask Maven to run your Java class with the required dependencies available. Use the following command (adapted as necessary):
mvn exec:java -Dexec.mainClass="com.example.Main"
See http://www.mojohaus.org/exec-maven-plugin/usage.html for more details.
I'm developing a selenium and TestNG project and I want to use Jenkins to run tests from .bat files.
I found some tutorials but all of them using Eclipse IDE. eg: http://learn-automation.com/selenium-integration-with-jenkins/
In Intellij IDEA I couldn't find any lib (jar files) folder and the file structure is slightly different than the Eclipse.
I was able to run through a POM.XML file but I really want to know how to build .bat file to run my testng.xml file. (From the beginning)
Compiling and Running java project from command line without any build automation tools (ANT, Maven, gradle) could get really complicated, however you can do that.
To compile and run I guess following should work.
To compile, I am not sure where class file will be generated but you can check help of javac command.
javac -classpath "[jarname with specified path]" [java filename]
This will execute the class files.
java -cp "path-to-jar/testng.jar:path_to_your_test_classes" org.testng.TestNG testng.xml
Now you can use this two command in batch file or directly use in jenkins.
However the easy and better approach will be to use Maven. There are lots of online blogs and video tutorial about how to setup up Maven project or convert existing project into maven.
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 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