Java - how to run console program in windows - java

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.

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.

How do you install MP3agic?

MP3agic: https://github.com/mpatric/mp3agic
Example.java: https://github.com/mpatric/mp3agic-examples/blob/master/src/main/java/com/mpatric/mp3agic/example/Example.java
I have been programming in java for a few years but have never installed anything extra relating to java (like MP3agic) before, and I have absolutely no idea how to do it. Reading up on websites it seems like it should be as easy as double-clicking on the .jar file, but when I do that with MP3agic I still don't have any luck.
Listed on the link above are brief instructions on how to install it: download and install maven (which I've done correctly), run "mvn lean package" in CMD, then in the newly created 'target' folder there are three jars: mp3agic-0.8.5-SNAPSHOT/SNAPSHOT-javadoc/SNAPSHOT-sources.jar.
At this point I double clicked on them all and assumed it was installed, but when I tried to run an Example.java file made for MP3agic (link at top), Java couldn't compile it.
I have tried added the path of my 'target' folder in the Path Environment Variable, but that hasn't worked. I also don't use something like Eclipse for programming (just Notepad++ and CMD, although that's likely to change)
You have to specify the class path during compilation. (I donot use IDE , I use plain CMD + Notepad++)
write something like this :
javac -cp mp3agic-0.9.1-SNAPSHOT.jar; Example.java
provided both of them are in the same dir.
Worked for me
For people like me:
Maven Installation:
Download Maven (Zip File)
Unzip Maven (anywhere)
Add Maven to system eviorment variables (help)
Get MP3agic.jar
Download Zip
Unzip MP3agic (anywhere)
Open cmd in MP3agic path
run "mvn clean install"
Include MP3agic.jar (IntelliJ)
Open project structor
Add Library -> Choose MP3agic.jar

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 .

Compiles with NetBeans but can't compile or run from bash

I have been researching this for weeks and can't seem to get it figured out.
I have a Java program that I have written using NetBeans. It has several imports or .jar files it relies on. It runs fine in NetBeans. But I can't figure out how to call the .jar files and compile from the bash command line. I am using a Mac. I have read several posts on this and none so far have made sense to me. There are 26 imports being used in the program. I don't know if I need to use Ant or specify -CP or Classpath to compile. Surely I don't have to type each one of the .jar files out to compile this from the bash command line?
Java(TM) SE Runtime Environment (build 1.7.0_09-b05)
You should be able to simply use ant with the build.xml in the Netbeans project directory. It should "just work" for you. The project will likely not build WITHOUT Netbeans installed (if you tried moving the project to a different machine, for example), but with Netbeans, it should work out of the box.
If you don't have Ant installed, you'll need to install it.
Addenda:
To build it, if you have Ant installed, you should be able to simply go the project directory, where the build.xml file is, and type "ant", and it should build and put stuff in the dist directory.
If you go in to the dist directory and type java -jar yourapp.jar, it should run, because the manifest in the jar will point to the nearby lib jar files. If you want to distribute the app, there are different options for java, such as making a Mac compatible application, or a Windows EXE, you'll need to search for those. Or you can simply distribute the contents of the dist directory and write a script to do that whole java -jar yourapp.jar command.
If you are using Java 1.6 or above, you can toss all 26 jar files into a folder and simply add
-cp folder_name/*
as your classpath argument.
Some other options are 1.) type all 26 jar files on the command line (using the -cp argument as you have mentioned) 2.) use Ant or Maven or some other build tool and list those 26 jar files in the config file for said build tool or 3.) write a quick-and-dirty shell script that will set the CLASSPATH environment variable for you and then run your javac command.

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.

Categories

Resources