How to build/run a java project on linux system using bash? - java

So I have a java project in intellij right now. I'm trying to get it to run through ssh on a linux system.
I've uploaded the class files, however The project will not run unless I take out all the package declarations and rebuild the project.
Whats the correct way to build/run a java project using bash?

The only way to run a java program is to call the java command-line.
To build it is javac but if you hae uploaded the class files, it means that it is already compiled

make sure you have a entry main method in one of your java file, then use java xxxx -cp yourjarpaths to start your program. You can also use ant or maven to start your project, in bash, you can run "ant xxx" or "maven xxxx" to start the jvm

While you can use the command line to build and run the java programs using javac and java commands, you should consider using Maven or Ant or any other build manager for your project. This will be especially useful when you start having external dependencies in your projects.

Related

I'm compiling and running fine my java project inside VS Code, but how can I compile it and run it in a different machine?

I have this organization inside VS Code: https://imgur.com/a/HXWo1VF
I just click on the "play" button and the app opens just fine. The command that VS Code use to run it is:
$ cd "/home/allexj/Dropbox/ingegneria_del_software/Link to ing_del_softw/codice/VipagePharma/vipagepharma" ; /usr/bin/env /usr/lib/jvm/java-11-openjdk/bin/java #/tmp/cp_8o2vekaqsyood6mnlra3x7qpf.argfile -m com.vipagepharma/com.vipagepharma.App
The build command is not showed, so I don't know how VS Code build it and I am newbie so I'm kinda poor in package and compiling stuff.
Inside /tmp/cp_8o2vekaqsyood6mnlra3x7qpf.argfile there is:
-cp "/home/allexj/.m2/repository/org/openjfx/javafx-controls/13/javafx-controls-13.jar:/home/allexj/.m2/repository/org/openjfx/javafx-graphics/13/javafx-graphics-13.jar:/home/allexj/.m2/repository/org/openjfx/javafx-base/13/javafx-base-13.jar:/home/allexj/.m2/repository/org/openjfx/javafx-fxml/13/javafx-fxml-13.jar" --module-path "/home/allexj/Desktop/ing_del_softw/codice/VipagePharma/vipagepharma/target/classes:/home/allexj/.m2/repository/org/openjfx/javafx-controls/13/javafx-controls-13-linux.jar:/home/allexj/.m2/repository/org/openjfx/javafx-graphics/13/javafx-graphics-13-linux.jar:/home/allexj/.m2/repository/org/openjfx/javafx-base/13/javafx-base-13-linux.jar:/home/allexj/.m2/repository/org/openjfx/javafx-fxml/13/javafx-fxml-13-linux.jar"
How can I build and run the same code in another machine? I don't know how VS Code has built everything, so I don't know how to do it in a portable way. Say you cloned this repo: https://github.com/All3xJ/VipagePharma/tree/main and you have to build&run it. How to do that?
You can convert project to a Maven project or create a jar file using jar cf jar-file input-file(s) and then run jar file using java -jar <jar-file> command
Guide to Creating and Running a Jar File in Java
Creating a JAR File
You can export your build to JAR from the projects view or by running the command Java: Export Jar....
You can refer to the document for more details.

Java - how to run console program in windows

Hi I wrote wordquiz program on Java. Using Eclipse in Unix.
In my linux machine it works fine.
Here is a source code https://github.com/HighlanderGe/Words
So, only basic package is used.
In windows compilation of such code as jar doesn't run. Neither in Mac.
As I guess problem is that in linux it is made to run from console, and console is comething very native for linux, but in Windows and I think in Mac too, cmd should be called, and from there it somehow to run.. but I bet cmd has no idea what is java. So some java console is needed for it?
The problem is not with the Mac or Windows, the problem is that you did not setup your workspace in Eclipse the same way on your different computers.
You can build your program on the command line in all environments in the same way. You just have to know the right steps.
First of all, there's an error in your code on line 25 in WordDatabase. Instead of:
dictionary = new ArrayList<>();
it should be:
dictionary = new ArrayList<String>();
After that, you can build your code like this:
javac -d . *.java
And run it like this:
java wordquizz/Wordquizz
This should work in any system that has Java, you just need to figure out how to setup your workspace in Eclipse the same way on your different computers.
UPDATE
I forked and converted your project to a Maven project:
https://github.com/janosgyerik/StackOverflow-Words
After you clone this to your PC, you can import into Eclipse using the File | Import... menu, and then Existing Maven projects option. It should work on all operating systems.
Maven is a recommended tool for building Java projects and it's a good thing to learn. After you install maven, you can build the project with:
mvn compile
You can package the project into a jar file with:
mvn package
You can run your code with either of these commands:
# needs 'mvn compile' first to generate classes
java -cp target/classes/ wordquizz.Wordquizz
# needs 'mvn package' first to generate the jar
java -cp target/wordquizz-1.0-SNAPSHOT.jar wordquizz.Wordquizz
If you like these improvements, merge from my repo soon. I won't keep it forever, I will delete it at some point.
UPDATE 2
To make a jar executable, you need to add inside a manifest file like this:
Main-Class: wordquizz.Wordquizz
You create the jar file with a command like this:
jar cvfm package.jar manifest.txt wordquizz/*.class
I updated my GitHub repository, so that now if you run mvn package, it will automatically add the right manifest, and the generated jar file will be executable.
If you installed java under windows you have to adjust the Path-variable, so the cmd knows where the java executable is. A good tutorial on how to set this up you can find here:
http://docs.oracle.com/javase/tutorial/essential/environment/paths.html
After that you can simple go to the directory your source code is in and use the same commands as under linux to compile and run your application.

Executing Eclipse Selenium Webdriver Java Code in Command Line?

I'm using Windows Server 2003,and I'm building a webpage and I want a particular testcase(a Java code) to execute when I click a button in that webpage(the webpage is in same server). SO first I have to find a way to run this eclipse java codes in command line.
On doing some research I found I need to install Maven or Ant. But I know this is not really required. I'm not sure which way to go. I prefer not using Maven or Ant. Can someone please tell me the steps to execute These tests in Command line. ALso the environment variables to add Because I'm unable to find QTjava.zip file in jre7 /lib/ext/ folder.
Have a look at the javac and java command line options. Heres what I did to run selenium tests without requiring eclipse or maven/ant.
this compiles your class, with the jar file (i put it in the same directory, but ideally you will put the full path in the qoutemarks:
javac -classpath "selenium-server-standalone-2.33.0.jar" Example.java
Then you can run the generated class via:
java -classpath ".;selenium-server-standalone-2.33.0.jar" Example
Hope that helps.

Compile and export in java via Terminal

I have a Java program in Eclipse on Mac currently, and I normally have to use multiple clicks just to export my code into a .jar file to test on my server. I would like to automate the process via terminal.
Basically, I compile my code usually by selecting the project
Export as Runnable JAR file
Select library handling: extract required libarires into generated JAR
Select export destination and hit done.
How can I do this via terminal? I assume this would first require me to compile the Java file, then to convert it to jar is a whole another step.
Help would be much appreciated.
You can create shell script that does it. This technique is obsolete since ~1998. So, use one of popular build tools. If you are starting now take a look on Gradle. Although there are a lot of other tools: good old ant, maven, buildr, ivy etc.
You can script all these activities using a build script. Several libraries exist for this, but Apache Ant is a good place to start. Ant build scripts can be run from command line or within eclipse, and will do all compilation, packaging and (some) deployment for you with a single command.
http://ant.apache.org/
Create default entry point manifest file as in : http://docs.oracle.com/javase/tutorial/deployment/jar/appman.html
Open Terminal and create an executable jar file like this:
Sample Script:
#!/bin/bash
# set CLASSPATH if needed
cd workspace/src
javac -d . *.java
jar tf exported.jar .

Application compiles and builds on Netbeans but not as an independent jar file?

Is it possible that your application compiles and builds flawlessly in Netbeans but when you build it and run it outside of Netbeans you get heaps of Exceptions??! (keeping in mind that the all the libraries and dependencies are ported along with the actual jar file).
I run the followng command
ant -f run
and my program runs flawlessly as it's supposed to, however, where I run the following command to actually build a jar file,
ant -f jar
and when I run the jar file, my application runs but does not perform the tasks it does when it is launched using the first command.
Any ideas why this is happening??! I'm totally lost with this behaviour and can't find why!
If the exceptions are ClassNotFound, then it means that you're missing JARs in the CLASSPATH. NetBeans is sorting it out for you, but when you run outside of it you have to manage it yourself.
keeping in mind that the all the libraries and dependencies are ported
along with the actual jar file
I'm not sure what the word "ported" ends up looking like for you, and without any real information I'm guessing, but your app isn't packaged correctly.
How are you attempting to build your project outside of NetBeans?
Inside NetBeans, if you created the project from within NetBeans, I believe that most of the time NetBeans will create all of its (by default) Ant targets in a file called nbbuild.xml.
Thus, if you're attempting to compile your project outside NetBeans from the command-line (however it exists on your system), you would then need to use the -f nbbuild.xml argument to the ant program to use the NetBeans-generated Ant file. So your command-line might look like this:
ant -f nbbuild.xml compile
Substitute, of course, the target you desire to run for compile in the example above.

Categories

Resources