How to run Solaris 11 or Cloud Solaris 11, Android Java? Or i can use the same Solaris 11 java. This is the script i was trying which works in Solaris 11 but not in Android.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class HelloNonsenseDroid
{
private static String myInput = "no";
public static void main(String[] args) throws IOException
{
System.out.print("Q. Are you Xroid? Answer. ");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
myInput = br.readLine();
if (myInput.equals("yes"))
{
System.out.println("Yes! you are genius");
} else {
System.out.println("No! what in the earth, is No! try to learn to say \"Yes, we can\"");
}
}
}
You can't just run normal Java console apps on android. Android has it's own GUI system to show "windows" and other GUI elements. You need to use those to show content to users.
Start here: http://developer.android.com/guide/index.html
Related
I'm trying to create a scenario on my Macbook with Mojave in Anylogic, which is part of agent-based simulation with many different tools. My idea is to connect Anylogic via Java Interface to Eclipse.
The main problem is, that Anylogic somehow does not respond.
I have tried many different codes with sockets, but could not find one, which worked for Anylogic. I'm using the free version of Anylogic and created a Java Interface under my Main Project. To run the Java Interface I press right-click on the file and select 'run with Java Editor'
In comparison to that I create two files in Eclipse, with one being the Server and one the Client and it worked.
so this is my Eclipse, which I guess should be fine https://www.minpic.de/i/7wbk/nv00b
this is my main model in Anylogic https://www.minpic.de/i/7wbn/pzuut
and there is the Java interface with the server code in it. https://www.minpic.de/i/7wbo/1mxsl4
Im pretty new to coding so hopefully you guys can help me.
public class server{
public static void main(String[] args) throws IOException {
ServerSocket ss = new ServerSocket(4995);
Socket s = ss.accept();
System.out.println("Client connected");
DataInputStream dout = new DataInputStream(s.getInputStream());
BufferedReader br = new BufferedReader (new InputStreamReader (System.in));
while(true) {
String yoo = dout.readUTF();
System.out.println("client" + yoo);
if(yoo.equalsIgnoreCase("exit"));
break;
}
ss.close();
}
}
public class client{
public static void main(String[] args) throws IOException {
Socket s = new Socket("localhost",4995);
DataOutputStream dout = new DataOutputStream(s.getOutputStream());
BufferedReader br = new BufferedReader (new InputStreamReader (System.in));
while (true)
{
String so= br.readLine();
dout.writeUTF(so);
System.out.println("client" + so);
if(so.equalsIgnoreCase("exit"));
break;
}
s.close();
}
}
I was expecting the consoles of both programs to show me messages I have send via the console, but neither of the programs show me the messages of what I wrote in the other program.
The Java code itself is fine, at least for creating a simple connection.
For the server side in Eclipse, you can leave it like that.
For the client side in AnyLogic however, there is an issue:
You can't run the code directly like this, because you have a main method in there. AnyLogic is no "normal" Java IDE like Eclipse, it is a very specific IDE. It automatically creates you exactly one project and puts everything in there that is needed to run it, including one main method. This means you don't need a second main method. You rather have to make your client become "part" of the bigger programm that AnyLogic is building for you. When you clicked on "Open with Java Editor" that only showed you the code, you cannot run any code like that in AnyLogic!
Therefore we do the following steps:
Create of a Java class (a simple one, without main method) Client in AnyLogic
Add a function to the class to start the client procedure (before it was triggered by it's own main method)
Create an instance from the Class Client
The class code, already including the function is the following:
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
public class Client implements Serializable {
public Client() {
}
public void activate() {
try {
Socket s = new Socket("localhost",4995);
DataOutputStream dout = new DataOutputStream(s.getOutputStream());
BufferedReader br = new BufferedReader (new InputStreamReader (System.in));
while (true)
{
String so= br.readLine();
dout.writeUTF(so);
System.out.println("client" + so);
if(so.equalsIgnoreCase("exit"));
break;
}
s.close();
}
catch(IOException e) {
System.out.println(e);
}
}
/**
* This number is here for model snapshot storing purpose<br>
* It needs to be changed when this class gets changed
*/
private static final long serialVersionUID = 1L;
}
Creating the instance and activating the client can be done with this code, add it for example in a button or the OnStartup code of the AnyLogic Main Agent:
Client client = new Client();
client.activate();
Class:-
=====================
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class TestPowershell {
public static void main(String[] args) throws IOException
{
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec("cmd powershell \"\\Test\\Powershell\\powershell.ps1\" ");
proc.getOutputStream().close();
InputStream is = proc.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader reader = new BufferedReader(isr);
String line;
while ((line = reader.readLine()) != null)
{
System.out.println(line);
}
reader.close();
proc.getOutputStream().close();
}
}
I am trying to execute a powershell file by using java in linux environment , i am getting exceptions (above i attached class and exceptions), kindly provide me a test class which can execute powershell script file in linux. Thanks in advance
1st download the FreeSSHD http://www.freesshd.com/?ctt=download in your windows(server). make sure run it as Administrator.
for setup FreeSSHD follow this URL http://www.techrepublic.com/blog/tr-dojo/set-up-a-free-ssh-server-on-windows-7-with-freesshd/ after setup you can ssh that windows system from linux or using putty.
to execute powershell script from linux to remote windows system using java
package com.sysvana.router.config;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;
public class Test {
static String hostname = "10.1.10.60";
static String username = "administrator";
static String password = "P#ssw0rd";
public static void main(String[] args) throws IOException {
Connection conn = new Connection(hostname);
conn.connect();
boolean isAuthenticated = conn.authenticateWithPassword (username, password);
if (isAuthenticated == false){
System.out.println("authentication failed");
}
System.out.println(isAuthenticated);
Session sess = conn.openSession ();
sess.execCommand ("powershell C:/Users/Administrator/Desktop/test.ps1");
InputStream stdout = new StreamGobbler (sess.getStdout ());
BufferedReader br = new BufferedReader (new InputStreamReader (stdout));
while (true)
{
String line = br.readLine ();
if (line == null) break;
System.out.println (line);
}
System.out.println ("Exit code" + sess.getExitStatus ());
sess.close ();
conn.close ();
}
}
use Ganymed SSH-2 jar http://www.ganymed.ethz.ch/ssh2/
Your problem is that you're not actually trying to run PowerShell. You're using this:
runtime.exec("cmd powershell \"\\Test\\Powershell\\powershell.ps1\" ")
which means it's trying to run a file called cmd, which is not part of Linux.
Also, in another part of the code, you're trying to run powershell.exe. Typically, Linux programs don't have a .exe extension. You may have ported your Java application from Windows, but you need to be aware that Linux is different. It doesn't come with Powershell as standard, and doesn't use .exe file extension.
You can install PowerShell at https://github.com/PowerShell/PowerShell.
On Linux, PowerShell (once installed from the above) is invoked with the pwsh command. You will need to change your Java program accordingly.
Thanks for all your answers.
Finally, I got to know while working with PowerShell we should run the script in windows OS only because Microsoft is the owner for PowerShell and they give more features in Windows OS.
what I did is, I ran the script in Windows OS & generated a CSV file and kept in SFTP folder, by using java I loaded my file and processed my next process.
I am trying to play a song (mp3 file) in java. I have been looking around for a few hours now and none of the ways I found worked properly.
public void play()
{
String song = "song.mp3";
Media track = new Media(song);
MediaPlayer mediaPlayer = new MediaPlayer(track);
mediaPlayer.play();
}
I have tried doing that but it gives me errors.
I have imported JMF and JLayer.
I have also read other questions that are like this one on this forum and none of them have helped me.
I just need a hand to help play an mp3 file.
The easiest way I found was to download the JLayer jar file from http://www.javazoom.net/javalayer/sources.html and to add it to the Jar library http://www.wikihow.com/Add-JARs-to-Project-Build-Paths-in-Eclipse-%28Java%29
Here is the code for the class
public class SimplePlayer {
public SimplePlayer(){
try{
FileInputStream fis = new FileInputStream("File location.");
Player playMP3 = new Player(fis);
playMP3.play();
}catch(Exception e){System.out.println(e);}
}
}
and here are the imports
import javazoom.jl.player.*;
import java.io.FileInputStream;
For this you'll need to install Java Media Framework (JMF) in your PC. One you have it installed,then try this piece of code:
import javax.media.*;
import java.net.*;
import java.io.*;
import java.util.*;
class AudioPlay
{
public static void main(String args[]) throws Exception
{
// Take the path of the audio file from command line
File f=new File("song.mp3");
// Create a Player object that realizes the audio
final Player p=Manager.createRealizedPlayer(f.toURI().toURL());
// Start the music
p.start();
// Create a Scanner object for taking input from cmd
Scanner s=new Scanner(System.in);
// Read a line and store it in st
String st=s.nextLine();
// If user types 's', stop the audio
if(st.equals("s"))
{
p.stop();
}
}
}
You may run into unable to handle formaterror, that is because Java took out the MP3 support by default (pirate copyright issue), you are required to install a “JMF MP3 plugin” in order to play MP3 file.
Go Java’s JMF website to download it
http://java.sun.com/javase/technologies/desktop/media/jmf/mp3/download.html
To be sure that you are using a supported format file, check here:
http://www.oracle.com/technetwork/java/javase/formats-138492.html
If you are using windows7, you may have to read this as well:
https://forums.oracle.com/forums/thread.jspa?threadID=2132405&tstart=45
How about JavaFX application-
import java.net.URL;
import javafx.application.Application;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.stage.Stage;
public class VLC extends Application {
void playMedia() {
String mp3 = "00- Tu Hi Mera.mp3";
URL resource = getClass().getResource(mp3);
System.out.println(resource.toString());
Media media = new Media(resource.toString());
MediaPlayer mediaPlayer = new MediaPlayer(media);
mediaPlayer.play();
}
public static void main(String args[]) {
new VLC().playMedia();
}
#Override
public void start(Stage stage) throws Exception {
}
}
I have this code :
package test;
import java.io.File;
import javax.media.Format;
import javax.media.Manager;
import javax.media.MediaLocator;
import javax.media.Player;
import javax.media.PlugInManager;
import javax.media.format.AudioFormat;
public class AudioTest {
public static void main(String[] args) {
Format input1 = new AudioFormat(AudioFormat.MPEGLAYER3);
Format input2 = new AudioFormat(AudioFormat.MPEG);
Format output = new AudioFormat(AudioFormat.LINEAR);
PlugInManager.addPlugIn(
"com.sun.media.codec.audio.mp3.JavaDecoder",
new Format[]{input1, input2},
new Format[]{output},
PlugInManager.CODEC
);
try{
Player player = Manager.createPlayer(new MediaLocator(new File("1.mp3").toURI().toURL()));
player.realize();
player.start();
}
catch(Exception ex){
ex.printStackTrace();
}
}
}
i'm trying to play an mp3 file, mp3plugin is added to the project lib as well as the jmf jar.
there's no error on the console but cant hear a sound.
the file is not playing.
.wav files are playing fine.
any idea ?
JMF is a bad option. The project was abandoned long time ago.
I have answered a similar question here:
Java - Error when trying to use mp3plugin for playing an mp3 file
it might be usefull for you - Im using Java Sound
The following is all I need to play music.
public static void main(String args[]) throws NoPlayerException, CannotRealizeException, IOException {
MediaLocator ml = new MediaLocator((new File("roar_of_future.mp3").toURL()));
Player player = Manager.createRealizedPlayer(ml);
player.start();
}
So please make sure mp3plugin.jar is in the classpath and your javasdk is Java 8 (32bit) or 7 (32bit) because JMF is not working on Java 9 and above.
First, I would like to mention that I also had this problem when 2.2 was still beta (forced me to revert back to 2.1.1).
Installed JavaSE 7u6 today (comes bundled with JavaFX 2.2 stable). NetBeans was
able to automatically detect the Default JavaFX Platform.
Created a new JavaFX Application project (tried the FXML derivative with the same result as well). Tried this piece of code:
package javafxapplication;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import javafx.application.Application;
import javafx.stage.Stage;
public class JavaFXApplication extends Application
{
public static void main(String[] args)
{
launch(args);
}
#Override
public void start(Stage primaryStage)
{
System.setProperty("socksProxyHost", "127.0.0.1");
System.setProperty("socksProxyPort", "9050");
try
{
URLConnection conn = new URL("http://www.wikipedia.org").openConnection();
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = br.readLine()) != null)
{
System.out.println(line);
}
}
catch (Exception e) { e.printStackTrace(); }
System.exit(0);
}
}
And it works. Without spewing an error that no connection could be made due to bad socks proxy settings (there is nothing running on that port on my machine). These properties are silently ignored and the connection occurs directly. Is this a bug? I've tested this on 2 machines running Win7 x64. Does not happen on 2.1.1.
JavaFX 2.2 introduced support for system proxy (see http://javafx-jira.kenai.com/browse/RT-21705).
It may interfere with socks proxy settings. You can try remove your system proxy or try to add next to JVM options: -Djavafx.autoproxy.disable=true