[Java] passaggio parametri al main con netbeans

ariannaari

Nuovo Utente
28 Set 2010
22
10
0
ragazzi salve, ho un problema. Non riesco a passare parametri al metodo main con Netbeans.

Ho 1 main class e una classe:
-lampadine.Lampadine (main)
-lampadine.lampadina

il codice della main class è il seguente:

package lampadine;
class Lampadine
{
public static int charToInt(char c)
{
if (c == '0') {return 0;}
if (c == '1') {return 1;}
if (c == '2') {return 2;}
if (c == '3') {return 3;}
if (c == '4') {return 4;}
if (c == '5') {return 5;}
if (c == '6') {return 6;}
if (c == '7') {return 7;}
if (c == '8') {return 8;}
if (c == '9') {return 9;}
else {return -1;} // indica che c non è una cifra
}
public static int stringToInt(String s)
{
int n = 0;
for (int i=0; i < s.length(); i++)
{
int cifra = charToInt(s.charAt(i));
if (cifra < 0) {
break;
} // esce dal ciclo for
n = n*10 + cifra;
System.out.println("valore"+n);
}
return n;
}
public static void main(String[] args)
{System.out.println("lunghezza="+args.length);
if (args.length < 1)
{System.out.println("Errore");}
else{

int n = stringToInt(args[0]);

System.out.println("Costruzione dell’array.");
Lampadina[] tutte = new Lampadina[n];
System.out.println("Costruzione delle lampadine.");
for (int i=0; i < tutte.length; i++) {
tutte = new Lampadina();
}

}
}
}

e il codice della classe lo tralascio perchè l'errore viene rilevato prima di chiamare la classe.

Allora, usando Netbeans ho visto che per passare parametri al main devo cliccare col destro sul progetto, scelgo la main class e nel campo arguments ho provato a scrivere una stringa (ho provato con "ciao", 'ciao' o ciao...per vedere se fosse un problema di apici), ma quando vado a eseguire mi dice che la lunghezza di args è 0, in tutti e 3 i casi. Cosa c'è che non va? sapete aiutarmi?
 
  • Like
Reactions: ottofonsuppost

Slyfer

Utente Attivo
4 Dic 2010
65
21
0
Ciao, qui trovi un esempio

http://netbeanside61.blogspot.it/2009/02/using-command-line-arguments-in.html

E la soluzione al problema che sembri avere anche tu

You may have had the same problem that I had...

The path to your run-time command line args is stored in the netbeans private.properties file
as "application.args=".

You may have overridden your private.properties file with an entry in your build.xml of

target name="-init-private" depends="-pre-init"

If you have, then args[] won't get picked up at runtime unless you create an overriding entry in your project.properties file to replace the one that should be picked up from your private.properties.

Hope this might help
 
Ultima modifica:
  • Like
Reactions: ottofonsuppost

ariannaari

Nuovo Utente
28 Set 2010
22
10
0
Mmm...sapresti aiutarmi? Non ho capito molto bene cosa e come devo fare!

sperando di aver trovato il giusto file private.properties, il contenuto è:

application.args="ciao"
compile.on.save=true
do.depend=false
do.jar=true
javac.debug=true
javadoc.preview=true
user.properties.file=C:\\Users\\arianna mazzocchetti\\AppData\\Roaming\\NetBeans\\7.2.1\\build.properties

il file build.xml è:
<!--
You may freely edit this file. See commented blocks below for
-->
<!-- some examples of how to customize the build. -->
<!--
(If you delete it and reopen the project it will be recreated.)
-->
<!--
By default, only the Clean and Build commands use this build script.
-->
<!--
Commands such as Run, Debug, and Test only use this build script if
-->
<!--
the Compile on Save feature is turned off for the project.
-->
<!--
You can turn off the Compile on Save (or Deploy on Save) setting
-->
<!-- in the project's Project Properties dialog box. -->
<project name="ESEMPI_SWING" default="default" basedir=".">
<description>Builds, tests, and runs the project ESEMPI_SWING.</description>
<import file="nbproject/build-impl.xml"/>
<!--


There exist several targets which are by default empty and which can be
used for execution of your tasks. These targets are usually executed
before and after some main targets. They are:

-pre-init: called before initialization of project properties
-post-init: called after initialization of project properties
-pre-compile: called before javac compilation
-post-compile: called after javac compilation
-pre-compile-single: called before javac compilation of single file
-post-compile-single: called after javac compilation of single file
-pre-compile-test: called before javac compilation of JUnit tests
-post-compile-test: called after javac compilation of JUnit tests
-pre-compile-test-single: called before javac compilation of single JUnit test
-post-compile-test-single: called after javac compilation of single JUunit test
-pre-jar: called before JAR building
-post-jar: called after JAR building
-post-clean: called after cleaning build products

(Targets beginning with '-' are not intended to be called on their own.)

Example of inserting an obfuscator after compilation could look like this:

<target name="-post-compile">
<obfuscate>
<fileset dir="${build.classes.dir}"/>
</obfuscate>
</target>

For list of available properties check the imported
nbproject/build-impl.xml file.


Another way to customize the build is by overriding existing main targets.
The targets of interest are:

-init-macrodef-javac: defines macro for javac compilation
-init-macrodef-junit: defines macro for junit execution
-init-macrodef-debug: defines macro for class debugging
-init-macrodef-java: defines macro for class execution
-do-jar-with-manifest: JAR building (if you are using a manifest)
-do-jar-without-manifest: JAR building (if you are not using a manifest)
run: execution of project
-javadoc-build: Javadoc generation
test-report: JUnit report generation

An example of overriding the target for project execution could look like this:

<target name="run" depends="ESEMPI_SWING-impl.jar">
<exec dir="bin" executable="launcher.exe">
<arg file="${dist.jar}"/>
</exec>
</target>

Notice that the overridden target depends on the jar target and not only on
the compile target as the regular run target does. Again, for a list of available
properties which you can use, check the target you are overriding in the
nbproject/build-impl.xml file.


-->
</project>

Che devo fare?
 
  • Like
Reactions: ottofonsuppost

Slyfer

Utente Attivo
4 Dic 2010
65
21
0
Ciao, ma quando devi fare run fai:

- tasto destro run file
oppure
- tasto destro sul progetto e fai run?

La prima non funziona, la seconda si. Ti allego gli screen (a me funziona)

ciao

arguments.jpgrun.jpg
 
  • Like
Reactions: ottofonsuppost

ariannaari

Nuovo Utente
28 Set 2010
22
10
0
Ciao, ma quando devi fare run fai:

- tasto destro run file
oppure
- tasto destro sul progetto e fai run?

Grazie mille, era proprio questo l'errore, utilizzavo il primo metodo, col secondo funziona! :fonzie: Grazieeeee!! :byebye:
 
  • Like
Reactions: ottofonsuppost

ottofonsuppost

Utente Attivo
10 Mag 2016
170
13
18
Il metodo MAIN è un metodo speciale di JAVA che serve a stabilire l'inizio di esecuzione del programma; è di tipo VOID quindi non restituisce nessun valore; possiede un solo parametro che è un ARRAY di stringhe che rappresenta una RIGA DI COMANDO, cioè quello che viene inserito con la tastiera. Quindi dire "non riesco a passare parametri al metodo MAIN è un errore grave, visto che la riga di comando è una cosa, e passare parametri è un'altra cosa.
 
Ultima modifica:
Discussioni simili
Autore Titolo Forum Risposte Data
L java api_google Javascript 0
F NetBeans problema creazione progetto Java Windows e Software 0
Z [java] bufferizzare stream audio da mic Java 1
L java + Api di google Javascript 1
A [Cerchiamo] [Retribuito/a] "Java Solution Architect" Offerte e Richieste di Lavoro e/o Collaborazione 1
F Script java elenco alfabetico non funziona Javascript 3
C Serializzazione in java Java 1
M AIUTO ESERCIZIO JAVA Javascript 1
M Ripasso Java Offerte e Richieste di Lavoro e/o Collaborazione 0
F [OFFRO - RETRIBUITO] Sviluppatori JAVA Offerte e Richieste di Lavoro e/o Collaborazione 0
L leggere RGB di un pixel dello schermo in java Java 1
I Creazione programmino JAVA Offerte e Richieste di Lavoro e/o Collaborazione 0
F Aiuto java script Javascript 2
T [Java] tipi generici con esempio pratico Java 1
J File audio in java Java 0
V [JAVA] come integrare un software scritto in java su una pagina web? Java 4
C Java client / server Java 0
F [OFFRO][RETRIBUITO] PROGRAMMATORE JAVA Offerte e Richieste di Lavoro e/o Collaborazione 0
C [Java] testare un metodo con Junit Java 1
A [Java] caricare un url esterno senza utilizzo di iframe Java 0
S [OFFRO] Debug delle tue applicazioni Java Offerte e Richieste di Lavoro e/o Collaborazione 1
L [Java] Aggiungere elementi ad array JSON Java 0
B [Java] Paginazione in risposta HTTP Java 0
A [Java]Date diminuite di un giorno su db MySQL Java 0
K [Java] aiuto switch case Java 1
P [Java] limite destro di un JFrame Java 5
D [Java] far partire JProgressBar all'apertura di un JFrame Java 1
N [java con eclipse]metodo ricorsivo che accetta in ingresso un char e restituisce un int Java 0
A Verifica validità data in Java Java 2
L [Java] Errore json conversione Java 0
Drago73 [Java] leggere/scrivere txt server Java 0
M [Javascript] Java card Java 0
serena.cerutti posizioni aperte: PhP, Java, .Net Offerte e Richieste di Lavoro e/o Collaborazione 0
N [Java]problema jasper report dopo compilazione file .jar Java 0
N [Java] Piccolo jform per calcolo totale da 2 campi i double Java 0
N [Java] jbutton con funzioni Java 2
M [java] esercizio lunghezza array di stringhe Java 0
A [java] problema esercizio Java 0
filograndipad2 Esempi chiari e completi sul funzionamento degli eventi in Java Java 1
Raziel84 [Java] Combinazione elementi matrice Java 0
A [java] problema esercizio array Java 5
L da php a popup java PHP 2
W Piccolo sistema per gestire alcune periferiche, Java o altro linguaggio? Java 8
S Cerco programmatore java Offerte e Richieste di Lavoro e/o Collaborazione 0
Z [Java] Grassetto assente su font incorporato nel pacchetto jar Java 0
O [Java] Lettura da JSON File Java 0
Z [Java] Caratteri distorti e spaziature errate nelle stampe di componenti swing Java 0
W Aiutino per compilare o eseguire un App Java Java 0
S [JAVA] Geocoding Java Java 0
Andy56 Parametri funzioni in Java Java 0

Discussioni simili