How to run a simple MongoDB java program from a unix terminal - java

I am trying to execute a simple hello world program in MongoDB.
I have compiled my java file using
javac -classpath mongo-2.10.0.jar HelloWorld
what to do next. Should I create the jar for the classfile and execute or directly run the class file. I m new to MongoDB.

You have several options:
You can run it with:
java -cp mongo-2.10.0.jar:. HelloWorld
Use maven or gradle to build / run the project.
Create one jar out of your project files.
For simple testing, I would go with 1.

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.

Is there a way to run selenium Java scripts without installing Eclipse?

I built several selenium Java scripts and each class represents a test and I also built nice UI that allows the users to select which test they want to run. All the classes are under the same project/package
I am trying to deploy this UI to end users so that they can run it to select the tests without having to install Eclipse. This will make it so easy for me to deploy it to end users.
Is this double?
You can specify the instruction to run it from command line as follows
java -jar filename.jar
ex:
java -jar program1.jar
the program1 consist of the followings are:
program1.class file
Resource library file such as SeleniumRC Server.jar and Selenium Java client.jar file
This method is applicable for SeleniumRC execution. We can directly create the program1.jar file from eclipse using File->Export.
I haven't tried it. Hope this helps

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

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.

Creating build of a Java project

I have a stand alone project in my eclipse.There are few external jar as well.The programme is executing well and giving required result. Now i need it to run from command prompt.and i just want to run the class file generated by eclipse. how i will be able to run it.When i am tring to run it from commend prompt it is giving class not found exception.I also tried to make an executable jar,but that is also not running.do i need to create executable jar or i can simply run it from command prompt..tell me the way out..
thanks
koushik
A step by step guide at
I recommend you to go through jar, class path and other basic building blocks of java for eg check this about jar
try using this command java -cp . package.name.className

creating our own jar and using it in another project

I want to create our own jar which has some simple methods like a print method.
I created the jar of that java project and imported it into an other project I am building path also.
The problem is that I am able to create the object of the classes which reside in the jar but I am unable to call the methods of that class.
i am using eclipse 3.4 (Eclipse Ganymede (3.4))version
Sounds like if you are successfully building the JAR that you are not including it in the classpath when you compile / run your application. You can do that if you are compiling/running from the command line with the -cp or -classpath option. Both perform the same function.
To compile:
javac -cp .:PathToMyJar/MyJar.jar MyMainClass.java
To Run:
java -cp .:PathToMyJar/MyJar.jar MyMainClass
The above commands will look in the current directory ('.') and in the MyJar.jar file specified. On Windows you will separate the items in the classpath with a semicolon and on Linux/Unix/OS X you will use a colon.
If you are using an IDE you will need to somehow add the JAR file to the classpath for your project. How to do that is IDE specific.

Categories

Resources