#Running cmd commands

1 messages · Page 1 of 1 (latest)

grand warren
#

so my code doesnt work

public class Main {
    public static void main(String[] args) {
        String[] command = { "ls" };
        var r = Runtime.getRuntime();
        r.exec(command);
    }
}```
errors: ```
DiscordBot.java:5: error: unreported exception IOException; must be caught or declared to be thrown
        r.exec(command);
              ^
1 error
error: compilation failed```
gritty hatchBOT
#

<@&987246883653156906> please have a look, thanks.

rancid atlas
grand warren
#

huh

#

im new to java

rancid atlas
#

public static void main(String[] args) throws IOException {...

#

@grand warren

grand warren
#

ty

#
    public static void main(String[] args) throws IOException {
                                                  ^
  symbol:   class IOException
  location: class Main
1 error
error: compilation failed```
gritty hatchBOT
grand warren
#

@rancid atlas

grand warren
#

how.....

rancid atlas
#

at the top

#

import java.io.IOException;

#

@grand warren

#

and please use an IDE

#

don't do it manually

grand warren
#

huh

#

idk

rancid atlas
#

?

grand warren
#

wdym

rancid atlas
#

What are you using currently ?
VSCode ?

grand warren
#

ye

rancid atlas
#

Either install the full extension pack of java for vscode

#

or better, install IntelliJ

#

but don't that manually please

grand warren
#

im installing the java extention

rancid atlas
#

not just one of the extension

#

and IntelliJ is far better

grand warren
#

i cant find it

#

but i got the full java pack

#

dw

rancid atlas
grand warren
#

intellij

rancid atlas
grand warren
#

it doesnt output anything

rancid atlas
grand warren
#

oh ye

rancid atlas
#

you just asked to run ipconfig

#

but you didn't get the inputstream to read it

grand warren
#

ok thanks!

#

do yk how to turn this into a .jar file

#

do i just rename it

#

@rancid atlas

rancid atlas
# grand warren
new ProcessBuilder("ipconfig")
  .redirectIO()
  .start();

Try this

grand warren
#

?

#

i dont need to see the output

rancid atlas
rancid atlas
grand warren
#

?

#

i just need to run a python file for a mc server

#

cuz im better at python

rancid atlas
#

but why are you using java then ?

#

and why ipconfig ?

grand warren
#

for testing

#

ipconfig is for testing

rancid atlas
#

right

#

and why are you using java if you are better at python ?

grand warren
#

cuz

rancid atlas
#

my question is, what are you trying to do that require java ?

grand warren
#

i need to run a python program for a mcserver

rancid atlas
#

ah

grand warren
#

also

grand warren
#

how do i make folders

rancid atlas
#

Files.createDirectories(Path.of("myfolder"))

grand warren
#

and check if folders exist

rancid atlas
#

@grand warren also, which version of java are you using ? And which version of mc ?

grand warren
#

sorry @rancid atlas i went out with a friend

#

how do i change directory?

rancid atlas
#

this isn't cmd

grand warren
#

dw

rancid atlas
rancid atlas
rancid atlas
#

right

grand warren
#

ye i just need to make a folder with all the python files then execute the whole folder

rancid atlas
#

Then what's the problem ?

rancid atlas
rancid atlas
grand warren
#

bad

rancid atlas
#

import java.nio.file.*

#

if you have the java extension pack of vscode, it should auto import them

grand warren
#

tysm

rancid atlas
#

click the hint

grand warren
#

finally

rancid atlas
#

or inetllij

grand warren
#

can i execute all of the files in a folder

shy tide
#

Why not use IntelliJ which is free? It'll help you with all that.

rancid atlas
grand warren
#

so like run all of the files in the specified folder

rancid atlas
#

files of what ?

#

python ?

grand warren
#

python files

rancid atlas
#

do a loop

grand warren
#

..

#

idk how

shy tide
#

It's easier to look up a guide.

#

But the basic premise is:

for (int i = 0; i < 10; i++) {
  System.out.println(i);
}
grand warren
#

i find it easier to get actual people to helo

#

help*

shy tide
#

We do suggest looking up the basics.

grand warren
#

: (

rancid atlas
# grand warren ..
for(Path pyFile : Files.list(Path.of("myfolder")).filter(p -> p.endsWith(".py")).toList()) {
  ...
}
grand warren
#

ok ty

rancid atlas
# shy tide We do suggest looking up the basics.

The problem here is that he doesn't know java and doesn't want to use it, and use python instead
But since he is trying to use a minecraft server, he needs at least java in order to call python files that he coded

grand warren
#

precicely

#

: D

rancid atlas
#

In any other cases, yes I would say just learn java properly

grand warren
#

so is this correct

rancid atlas
rancid atlas
grand warren
#

oops

#

1 mo

rancid atlas
# grand warren

also the extension pack isn't active here otherwise some parts should be red

rancid atlas
rancid atlas
grand warren
#

i think i got it

#

@rancid atlas

rancid atlas
#

and please read my other messages

grand warren
rancid atlas
rancid atlas
grand warren
#

i did

rancid atlas
#

ah yes my bad

#

for the first one

#

but not the other

grand warren
#

?

rancid atlas
#

Also

grand warren
#

wdym both

rancid atlas
rancid atlas
#

and you are copy pasting the code twice

#

your if is pointless

grand warren
#

ok?

rancid atlas
#

paste the code here please

shy tide
rancid atlas
#

and the if is useless

grand warren
#

so...

rancid atlas
shy tide
grand warren
#
import java.nio.file.*;

public class Main {
    public static void main(String[] args) throws IOException {
        if (Files.isDirectory(Path.of("PyServer"))) {
            for(Path pyFile : Files.list(Path.of("PyServer")).filter(p -> p.endsWith(".py")).toList()) {
                new ProcessBuilder("python" + pyFile)
                .redirectIO()
                .start();
              }
        }
        else {
            Files.createDirectories(Path.of("PyServer"));
            }
        }
    
}```
gritty hatchBOT
# grand warren ```import java.io.IOException; import java.nio.file.*; public class Main { ...

Detected code, here are some useful tools:

Formatted code
import java.io.IOException;
import java.nio.file. * ;

public class Main {
  public static void main(String[] args) throws IOException {
    if (Files.isDirectory(Path.of("PyServer"))) {
      for (Path pyFile : Files.list(Path.of("PyServer")).filter(p -> p.endsWith(".py")).toList()) {
        new ProcessBuilder("python" + pyFile).redirectIO().start();
      }
    }
    else {
      Files.createDirectories(Path.of("PyServer"));
    }
  }
}
rancid atlas
#

wait no

grand warren
#

?

rancid atlas
#
import java.io.IOException;
import java.nio.file.*;

public class Main {
    public static void main(String[] args) throws IOException {
        Files.createDirectories(Path.of("PyServer"));
        for(Path pyFile : Files.list(Path.of("PyServer")).filter(p -> p.endsWith(".py")).toList()) {
            new ProcessBuilder("python", pyFile)
              .redirectIO()
              .start();
        }
    }
}

@grand warren

#

try this

grand warren
#

bad

rancid atlas
#

ah yes

#

but first

#

please make the java extension pack working

#

or use intellij

#

and stop using cmd

#

@grand warren

grand warren
#

k

rancid atlas
#

this error should appear in red directly in vsc

#

@grand warren

grand warren
#

downloading intellij

rancid atlas
#
import java.io.IOException;
import java.nio.file.*;

public class Main {
    public static void main(String[] args) throws IOException {
        Files.createDirectories(Path.of("PyServer"));
        for(Path pyFile : Files.list(Path.of("PyServer")).filter(p -> p.endsWith(".py")).toList()) {
            new ProcessBuilder("python", pyFile.toString())
              .redirectIO()
              .start();
        }
    }
}

@grand warren
Should work

grand warren
rancid atlas
grand warren
#

im installing intellij rn

rancid atlas
#

right

grand warren
#

gotta reboot

#

wait i gotta pay for intelliji

rancid atlas
#

no

#

there are two version

#

a paid one and a free one

#

on their website

#

scroll down

grand warren
rancid atlas
#

yes

#

that's the ultimate version aka paid

#

download the free one

grand warren
#

ah...

rancid atlas
#

scroll down

grand warren
#

ye i found it..

#

i got it

#

so do i just copy and paste the code @rancid atlas ?

rancid atlas
#

it should generate a main class and main method

grand warren
#

i got it up

rancid atlas
#

hmm

#

show the file tree at the left please

grand warren
shy tide
#

Click top right in the yellow bar to point it at the JDK you've installed.

rancid atlas
grand warren
#

where?

rancid atlas
#

no

grand warren
#

oh its just 1 file

rancid atlas
#

that's an image I took from the internet

rancid atlas
grand warren
rancid atlas
#

right

#

see

#

now you have one error

#

it is red

grand warren
#

wow

#

ok!

rancid atlas
#

so to fix this

#

use pyFile.toString() instead

grand warren
#

kk

rancid atlas
#

take a screenshot again

grand warren
rancid atlas
grand warren
#

java: cannot find symbol
symbol: method redirectIO()
location: class java.lang.ProcessBuilder

rancid atlas
#

hmm

#

ah my bad

#

it's
inheritIO()

#

not redirectIO()

#

@grand warren

grand warren
#

we all make mistakes 🙂

rancid atlas
#

are there any error now ?

grand warren
#

no red...

rancid atlas
#

right so

#

just a few tips

#

about intellij

grand warren
#

yaa

rancid atlas
#

if you start to write anything, it will try to autocomplete

grand warren
#

k

rancid atlas
#

it will try ti find all the method that can be called

#

it will try to autocomplete too

grand warren
#

oki 🙂

rancid atlas
#

and note that you can press alt+enter on pretty much anything to get some auto code change

#

like for example, you have a class you want to import

#

just press alt+enter on it

#

then select import

#

and finally

#

you can check the doc of everything by pressing ctrl+q on anything

#

@grand warren try it

grand warren
#

nrn

#

im testing the code

rancid atlas
#

right

#

so

#

@grand warren click on any of the green arrow to run the code

grand warren
#

k

#

ummmm

rancid atlas
grand warren
#

hold up

rancid atlas
#

well

grand warren
#

it doesnt run the files

rancid atlas
#

there are probably nothing in the PyServer folder you just created lol

grand warren
#

no i made 2

#

that make a dir

#

to show it works

#

but it doesnt run the files

rancid atlas
grand warren
#

it makes it in the wrong place

north saffron
#

these files look like text files

#

maybe the files are idk.py.txt

rancid atlas
rancid atlas
north saffron
rancid atlas
#

hmm

grand warren
rancid atlas
grand warren
#

its just intellej

#

both of then

#

them*

#

say

#

or smt like that

rancid atlas
#

?

rancid atlas
grand warren
#

yes

rancid atlas
#

so what's the problem ?

grand warren
#

there not running

rancid atlas
#

in the java code

#

do you enter the loop ?

#

I mean

#

does it find any file ?

#

can you place a println in the loop

grand warren
#

idk

rancid atlas
#

note that sout will autocomplete into System.out.println in IntelliJ @grand warren

#

so try to place a println in the loop

#

and print the path

grand warren
#

nuthin was found

rancid atlas
#

no print ?

grand warren
#

ye

#

no print

rancid atlas
#

then do

#

System.out.println(Path.of("PyServer").toAbsolutePath());
outside the loop, does it corectly match the folder @grand warren ?

grand warren
#

yes

rancid atlas
# grand warren

what if you temporary remove the .filter(...) ? does it find things ?

grand warren
#

yes

rancid atlas
grand warren
#

it just says file found!

#

thats what i set it as

rancid atlas
#

what does it show ?

grand warren
#

wait it just executed

rancid atlas
#

???

grand warren
#

omg it works!

rancid atlas
#

wtf

grand warren
#

ooh!

#

it found the files

#

so it ran them!

grand warren
#

@rancid atlas , ive just made it a jar file,but when i run my server, it doesnt make the folder

shy tide
#

Any errors? Did it crash? Are the rights set correctly?

grand warren
#

ik what i did wrong

#

@rancid atlas ive made it a mc plugin, but now its buggy

rancid atlas
#

you have to try catch

#

so remove the throws

#

alt+enter -> try catch the whole block

grand warren
#

i asked chatgbt

#

import org.bukkit.plugin.java.JavaPlugin;

import java.io.IOException;
import java.nio.file.*;

public final class PyserverPlugin extends JavaPlugin {

    @Override
    public void onEnable() {
        System.out.println("[PyServer] Loading PyServer...");
        try {
            Files.createDirectories(Paths.get("PyServer"));
            System.out.println("[PyServer] Located files:");
            Files.list(Paths.get("PyServer")).forEach(pyFile -> {
                System.out.println(pyFile.toString());
                try {
                    new ProcessBuilder("python", pyFile.toString())
                            .inheritIO()
                            .start();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            });
        } catch (IOException e) {
            System.out.println("Error creating directory or listing files: " + e.getMessage());
            e.printStackTrace();
        }
    }

    @Override
    public void onDisable() {
        // Plugin shutdown logic
    }
}```
#

the plugin doesnt even appear

#

@rancid atlas

rancid atlas
grand warren
#

no when i execute it, it just makes a jar file

rancid atlas
#

wdym

#

you are saying that this code creates a jar file ???

grand warren
#

well it runs another file

#

its weird

#

@rancid atlas I found the errors

rancid atlas
grand warren
#

das pom.xml

rancid atlas
#

also, does it work from intellij?

grand warren
#

ye

#

i clicked run

#

but it ran the pom

#

idk why

#

but when i made it a .jar file it just

#

didnt appear

rancid atlas
grand warren
#

idk

rancid atlas
#

please be more precise with your explanation

#

each time I have to decode them

grand warren
rancid atlas
#

did you run the jar manually ?

grand warren
#

when i ran /plugins in minecraft, the plugins didn't appear

#

no i put the .jar file into the plugins folder

rancid atlas
#

ah

rancid atlas
grand warren
#

cmd

#

i got the errors from cmd

rancid atlas
#

from the server ?

grand warren
#

no

#

java Pyserver.java

#

in cmd

rancid atlas
#

well yes

grand warren
#

ok lemme try smt

rancid atlas
#

wait a minute how is that related

rancid atlas
grand warren
#

i gtg, ill try again tmrw

grand warren
#

Hey @rancid atlas thanks for all the help with this! The plugin works now I'm just gonna make a command that lists all of the files and it will be done, tysm for all your help : D